Back to series
· 12 min read

Binary: Why Do Computers Use 0 and 1?


I. What Is Binary?

The number system we use in everyday life is decimal, with ten digits from 0 to 9. Computers, however, mainly use binary, which contains only two digits:

  • 0
  • 1

These two digits are called binary digits, also known as bits — in English, Bit, abbreviated as b.

Binary follows a counting rule in which every two units make one unit of the next place, while decimal carries over every ten.

For example:

Decimal Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
15 1111
16 10000

Binary is not a language invented specifically for computers; it is simply a numeral system with a base of 2.

1. Place Value in Binary

Any numeral system can express values through place value.

The decimal number 123 means:

123=1\times10^2+2\times10^1+3\times10^0

The binary number 1011 means:

1011_2=1\times2^3+0\times2^2+1\times2^1+1\times2^0

The result is:

8+0+2+1=11

Therefore:

1011_2=11_{10}

The subscript is used to distinguish the base:

  • 1011_2: binary.
  • 11_{10}: decimal.
  • B: the base of binary place value, 2.

2. Why Does Binary Have Only Two Digits?

Because the point of binary is not that it has few digits, but that it only needs to distinguish two states.

For example:

  • A switch being off and on.
  • Low voltage and high voltage.
  • A signal being invalid and valid.
  • Logical false and logical true.

As long as a system can reliably distinguish two states, it can use 0 and 1 to express information.

This is also the key reason binary is suited to computers.


II. Why Do Computers Choose Binary?

A computer is essentially an information-processing system built from electronic components.

CPUs, memory, caches, and digital circuits all need to express states through electronic signals. The choice of binary is closely tied to the physical characteristics of electronic components.

1. Electronic Circuits Can Easily Represent Two States

In digital circuits, logic states can be represented by voltage ranges.

For example, in a certain digital circuit:

Voltage range Logic state Binary representation
Low voltage Logic low 0
High voltage Logic high 1

Note here: a logic 0 does not necessarily mean there is absolutely no voltage, nor does a logic 1 mean the voltage is infinitely high.

Real circuits usually define certain voltage ranges as low level and high level respectively.

For example, a simplified digital circuit might use:

  • 0–0.8V: recognized as logic 0.
  • 2.0–3.3V: recognized as logic 1.

The exact thresholds depend on the chip's supply voltage and the logic circuit standard.

<|OPENAI|c3e1e7d2f83b49c6a55a3b9a2e31e8c4|IMAGE|>

2. Two states are easier to distinguish

Suppose an electronic system needs to represent ten states; the circuit must then be able to distinguish ten different signal levels.

This means:

  • More precise voltage control is required.
  • More precise signal detection is required.
  • It is more susceptible to noise interference.
  • It is harder to distinguish between different states.

If only two states need to be distinguished, circuit design is much simpler.

For example:

Low level               High level
    0                      1
    │                      │
    ▼                      ▼
┌────────┐              ┌────────┐
│ Logic 0│              │ Logic 1│
└────────┘              └────────┘

A relatively wide voltage range can be set between the two states, allowing the circuit to tolerate signal variations and noise to some extent.

3. Binary has strong noise immunity

When real electronic devices operate, they are affected by various types of noise, such as:

  • Electromagnetic interference.
  • Power supply fluctuations.
  • Signal crosstalk.
  • Temperature changes.
  • Component manufacturing tolerances.

Digital circuits do not require every signal to reach some absolutely precise voltage; instead, they determine which valid range the signal falls into.

As long as noise does not cause the signal to cross the logic decision threshold, the circuit can still correctly identify the state.

This threshold-based state determination gives binary digital circuits good stability.

4. Binary is suitable for building logic circuits

Binary can not only represent data but also participate directly in logic operations.

For example:

A B AND OR XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

These logic operations can be built using simple electronic switches.

By further combining logic gates, adders, comparators, registers, and arithmetic units can be built.

Therefore, binary is suitable not only for storing information but also for building computing systems.


III. The Relationship Between Binary and Digital Circuits

To understand binary, it is necessary to further understand how digital circuits implement 0 and 1.

Digital circuits are based on transistors.

1. What is a transistor?

A transistor is a semiconductor electronic component that can control current.

In digital circuits, transistors are often used as electronic switches.

It can control the output state based on the input signal:

  • On: allows current to flow.
  • Off: blocks current flow.

By combining large numbers of transistors, various digital logic circuits can be built.

Modern CPUs contain billions or even more transistors, which together perform data processing, logic decisions, and instruction execution.

<|OPENAI|e3d79a7f5c6e42e3b85d54d4a4e9b1c1|IMAGE|>

2. How do transistors represent binary?

This can be understood using a simplified switch model:

Transistor off                Transistor on

    Input                         Input
      │                             │
      ▼                             ▼
┌────────────┐                ┌────────────┐
│ Switch off │                │ Switch on  │
└────────────┘                └────────────┘
      │                             │
      ▼                             ▼
 Output state                  Output state
      0                             1

This is only a simplified model to aid understanding.

The logic state of a real digital circuit depends on the circuit structure, input voltage, and output voltage; it does not mean that the on and off states of all transistors can be directly equated with logic 1 and logic 0.

3. How are logic gates built?

Logic gates are the most basic logic operation units in digital circuits.

Common logic gates include:

Logic gate English Main function
AND gate AND Output is 1 only when all inputs are 1
OR gate OR Output is 1 when at least one input is 1
NOT gate NOT Inverts the input state
XOR gate XOR Output is 1 when the inputs are different
NAND gate NAND Inverts the result of the AND operation
NOR gate NOR Inverts the result of the OR operation

For example, the logic relationship of an AND gate:

Y=A\land B

Y is 1 only when both A and B are 1.

flowchart TD
    A["Input A"] --> C["AND gate"]
    B["Input B"] --> C
    C --> D["Output Y"]
    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E6DFF5,stroke:#B6A4D8,color:#51416F
    style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F

Logic gates can be further combined into complex circuits.

For example:

  • Multiple logic gates can form a half adder.
  • A half adder and other logic circuits can form a full adder.
  • Multiple full adders can form a multi-bit adder.
  • An adder and other arithmetic circuits can form an ALU.

This is the foundation that enables computers to perform complex operations using simple 0s and 1s.


4. How Does Binary Convert Numeric Values?

In programming, networking, operating systems, and hardware development, it is often necessary to convert between binary, decimal, and hexadecimal.

1. Binary to Decimal

Core method: multiply each bit by the corresponding power of 2, then sum the results.

For example:

1101_2

Expand:

1\times2^3+1\times2^2+0\times2^1+1\times2^0

Calculate:

8+4+0+1=13

Therefore:

1101_2=13_{10}

2. Decimal to Binary

Core method: repeatedly divide by 2, record the remainder each time, and finally arrange the remainders in reverse order.

For example, to convert decimal 13 to binary:

Operation Quotient Remainder
13 ÷ 2 6 1
6 ÷ 2 3 0
3 ÷ 2 1 1
1 ÷ 2 0 1

Read the remainders from bottom to top:

13_{10}=1101_2

Conversion process:

flowchart TD
    A["Input decimal integer"] --> B["Divide by 2"]
    B --> C["Record remainder"]
    C --> D{"Is the quotient 0?"}
    D -->|"No"| B
    D -->|"Yes"| E["Arrange remainders in reverse order"]
    E --> F["Get binary result"]
    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#FFF1D6,stroke:#E7C77E,color:#76551E
    style E fill:#E6DFF5,stroke:#B6A4D8,color:#51416F
    style F fill:#DCEFE5,stroke:#8EBFA5,color:#24563F

3. Binary to Hexadecimal

Conversion between binary and hexadecimal is very convenient because:

16=2^4

One hexadecimal digit corresponds exactly to four binary digits.

For example:

11010110_2

Group every four bits from right to left:

1101 0110

Convert each group separately:

  • 1101 corresponds to hexadecimal D.
  • 0110 corresponds to hexadecimal 6.

Therefore:

11010110_2=D6_{16}

Common correspondences between four-bit binary and hexadecimal:

Binary Hexadecimal Decimal
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15

4. Why do programmers often use hexadecimal?

Binary is suitable for computer processing, but it is inconvenient for humans to read.

For example:

11111111111111110000000010101010

Such numbers are long and not easy to identify and inspect.

Using hexadecimal can simplify the representation:

FFFF00AA

Both express the same set of binary data.

Common uses of hexadecimal:

  • Memory addresses.
  • Machine instructions.
  • Binary file analysis.
  • Network packets.
  • Color encoding.
  • Memory viewing in debuggers.

It should be noted that hexadecimal is merely a representation that is more convenient for humans to use; internally, computers do not change the binary basis of their operation because of it.


V. Bit and Byte: How Do Computers Organize Binary Data?

The smallest unit of binary data is the Bit.

However, a single Bit can only represent 0 or 1, so its expressive capacity is very limited.

Therefore, computers usually combine multiple Bits to represent richer data.

1. Bit: Binary Digit

Bit is the abbreviation of Binary Digit and represents one binary digit.

A single Bit has only two possibilities:

0 \quad 或 \quad 1

Two Bits can represent:

2^2=4

different states:

00
01
10
11

Three Bits can represent:

2^3=8

different states.

Generally speaking, n Bits can represent:

2^n

different states.

2. Byte: The Byte

A Byte is usually composed of 8 Bits:

1\ Byte=8\ Bits

For example:

01001000

This is a byte composed of 8 binary digits.

A byte can represent:

2^8=256

different states.

If represented as an unsigned integer, its value range is:

0\sim255

If represented using two's complement for an 8-bit signed integer, its value range is:

-128\sim127

3. Common Storage Units

Unit Meaning Conversion
bit bit Smallest binary unit
Byte byte 8 bit
KB kilobyte Usually 1000 Byte
MB megabyte Usually 1000 KB
GB gigabyte Usually 1000 MB
TB terabyte Usually 1000 GB

Computer memory and many software tools also frequently use these units:

Unit Conversion
KiB 1024 Byte
MiB 1024 KiB
GiB 1024 MiB
TiB 1024 GiB

It is important to distinguish decimal units from binary units:

  • GB usually means 10^9 Byte.
  • GiB means 2^{30} Byte.

This is why the capacity printed on a hard drive differs from the capacity shown by the operating system.

4. What is the difference between a Bit and a Byte?

The easiest point of confusion between the two is capitalization:

  • b: bit.
  • B: Byte.

For example:

  • 100 Mbps: 100 megabits per second.
  • 100 MB/s: 100 megabytes per second.

Ignoring protocol overhead and unit differences:

100\ Mbps\div8=12.5\ MB/s

Therefore, network bandwidth and file transfer speeds cannot be compared directly by their numeric values.


VI. How Does Binary Represent Integers?

Inside a computer, the decimal numbers we use in daily life are not stored directly; data is saved in binary encoding.

For integers, the most common representations are unsigned integers and signed integers.

1. Unsigned Integers

Unsigned integers can only represent non-negative numbers.

For example, an 8-bit unsigned integer:

00000000
00000001
00000010
...
11111111

Minimum value:

0

Maximum value:

2^8-1=255

Therefore:

0\leq x\leq255

2. Signed Integers

Computers need to represent negative numbers, so a binary encoding convention is required.

Modern computers generally use two's complement to represent signed integers.

Taking 8-bit two's complement as an example:

Decimal 8-bit two's complement
0 00000000
1 00000001
2 00000010
3 00000011
-1 11111111
-2 11111110
-3 11111101
-128 10000000

The range of an 8-bit signed integer:

-2^7\sim2^7-1

That is:

-128\sim127

3. How Is the Two's Complement of a Negative Number Calculated?

Take -5 as an example, using 8-bit binary representation.

Step one: get the binary form of the positive number 5:

00000101

Step two: invert each bit:

11111010

Step three: add 1:

11111011

So:

-5=11111011_2

The core value of two's complement is that it lets an adder circuit handle both positive and negative numbers uniformly.

For example:

  00000101
+ 11111011
-----------
1 00000000

Ignoring the carry beyond 8 bits, the result is:

00000000

In other words:

5+(-5)=0

4. Why is two's complement so important?

If a computer were designed with separate hardware for adding positive numbers and subtracting negative numbers, the circuitry would become much more complex.

Two's complement allows addition and subtraction to be implemented with the same adder.

Subtraction can be transformed into:

A-B=A+(-B)

This makes the CPU's integer arithmetic circuitry more uniform.


7. How Does Binary Represent Characters and Text?

Computers don't just need to handle numbers—they also need to handle text.

But a computer cannot directly understand the meaning of Chinese characters, English letters, or punctuation marks. It must use character encoding to map characters to numbers, and then convert those numbers into binary.

1. ASCII Encoding

ASCII is a character encoding standard that was widely used in the early days.

It uses 7 binary bits to represent 128 characters, including:

  • English letters.
  • Digits.
  • Common punctuation.
  • Control characters.

For example:

Character Decimal Hexadecimal Binary
A 65 41 01000001
B 66 42 01000010
C 67 43 01000011
a 97 61 01100001
0 48 30 00110000

The character A corresponds to decimal 65, which in binary is:

01000001

2. Unicode and UTF-8

ASCII cannot fully represent the characters of all languages in the world.

For example, Chinese, Japanese, Korean, and other languages all require larger character sets.

Unicode provides a unified encoding system for characters worldwide.

UTF-8 is an encoding scheme for Unicode that uses variable-length byte encoding.

In UTF-8, a single Unicode character may occupy:

  • 1 byte.
  • 2 bytes.
  • 3 bytes.
  • 4 bytes.

For example:

Character Unicode Code Point UTF-8 Bytes
A U+0041 41
中 U+4E2D E4 B8 AD
文 U+6587 E6 96 87

Therefore, the number of characters in a string is not necessarily equal to the number of bytes.

For example, the string:

A中

contains two Unicode characters, but after UTF-8 encoding it occupies 4 bytes.

3. The Text Encoding Process

flowchart TD
    A["User enters text"] --> B["Identify characters"]
    B --> C["Look up Unicode code points"]
    C --> D["Encode using UTF-8"]
    D --> E["Convert to byte sequence"]
    E --> F["Store or transmit"]
    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E6DFF5,stroke:#B6A4D8,color:#51416F
    style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style E fill:#FFF1D6,stroke:#E7C77E,color:#76551E
    style F fill:#F1F3F5,stroke:#CBD5E1,color:#334155

Here, three concepts need to be distinguished:

  • Character: the text that humans see.
  • Unicode code point: the number corresponding to a character in Unicode.
  • UTF-8: the rule that encodes code points into byte sequences.

8. How Does Binary Represent Images, Audio, and Video?

Modern computers need to handle not only numbers and text, but also images, sound, and video.

Ultimately, these kinds of data can also be converted into binary for storage and processing.

1. How Are Images Represented?

A digital image can be understood as a matrix made up of a large number of pixels.

Each pixel stores color information.

Taking an RGB image as an example, each pixel is typically made up of three color channels:

  • R: red.
  • G: green.
  • B: blue.

If each channel is represented with 8 bits, then:

8+8+8=24\ bit

A single pixel then requires 24 bits to represent its color.

For example, pure red:

R = 255 = 11111111
G = 0   = 00000000
B = 0   = 00000000

The corresponding RGB hexadecimal color is:

#FF0000

An uncompressed 24-bit RGB image of 1920 × 1080 theoretically requires:

1920\times1080\times24

That is:

49,766,400\ bit

Converted to bytes:

49,766,400\div8=6,220,800\ Byte

approximately 5.93 MiB.

Actual image files may also be encoded and compressed in formats such as JPEG, PNG, and WebP, so the file size is usually different from the size of the uncompressed pixel data.

2. How is audio represented?

Sound is essentially a continuously changing sound wave.

Computers need to use sampling and quantization to convert continuous sound into discrete numbers.

Two key concepts:

  • Sampling rate: how many times sound is sampled per second.
  • Bit depth: how many bits are used to represent amplitude in each sample.

For example:

Common CD audio parameters:

  • Sampling rate: 44.1 kHz.
  • Bit depth: 16 bit.
  • Number of channels: 2.

The theoretical data rate of uncompressed PCM audio is:

44100\times16\times2

That is:

1,411,200\ bit/s

approximately 1411.2 kbps.

3. How is video represented?

Video can be understood as a sequence of images played continuously, and it may also contain audio and other information.

Video usually includes:

  • Video frames.
  • Frame rate.
  • Resolution.
  • Color information.
  • Audio data.
  • Timestamps.
  • Encoding and compression information.

For example, 30 FPS means 30 frames are played per second.

If every frame is a 1920 × 1080 RGB image, the amount of uncompressed data would be enormous.

Therefore, video usually uses encoding technologies such as H.264, H.265, and AV1 to compress data.

4. Unified representation of multimedia data

Although images, sound, and video have different forms of representation, they can ultimately all be converted into binary data.

flowchart TD
    A["Information in the real world"] --> B["Digitization"]
    B --> C["Images"]
    B --> D["Audio"]
    B --> E["Video"]
    C --> F["Binary encoding"]
    D --> F
    E --> F
    F --> G["Storage devices"]
    F --> H["Network transmission"]
    F --> I["Computer processing"]
    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E6DFF5,stroke:#B6A4D8,color:#51416F
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style F fill:#FFF1D6,stroke:#E7C77E,color:#76551E
    style G fill:#F1F3F5,stroke:#CBD5E1,color:#334155
    style H fill:#F1F3F5,stroke:#CBD5E1,color:#334155
    style I fill:#F1F3F5,stroke:#CBD5E1,color:#334155

9. How does the CPU use binary to perform calculations?

The ultimate purpose of understanding binary is to understand how computers use it to execute programs.

The CPU contains a large number of digital circuits, and these circuits use binary signals to perform operations and control.

1. Binary addition

Binary addition is similar to decimal addition, except that it carries when the sum reaches two.

Basic rules:

Operation Result
0 + 0 0
0 + 1 1
1 + 0 1
1 + 1 10

Where:

1+1=10_2

This is equivalent to the following in decimal:

1+1=2

2. Half adder

A half adder is one of the basic circuits for implementing binary addition.

It has two inputs:

  • A.
  • B.

and two outputs:

  • S: sum.
  • C: carry.

Its logical relationships are:

S=A\oplus B
C=A\land B

The corresponding truth table is:

A B Sum S Carry C
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

3. Full Adder

A half adder can only handle two input bits and cannot directly handle a carry from a lower bit.

A full adder adds a carry input Cin, so it has three inputs:

  • A.
  • B.
  • Cin.

The outputs are still:

  • S.
  • Cout.

Formulas:

S=A\oplus B\oplus Cin
Cout=(A\land B)\lor(Cin\land(A\oplus B))

By combining multiple full adders, a multi-bit adder can be built.

4. The CPU's ALU

ALU is short for Arithmetic Logic Unit.

It is responsible for performing arithmetic and logical operations in the CPU, such as:

  • Addition.
  • Subtraction.
  • AND operation.
  • OR operation.
  • XOR operation.
  • Comparison.
  • Shift.

When the CPU executes an addition instruction, the instruction goes through the decode and execute stages, and the relevant control signals drive the ALU to complete the operation.

For example:

A = 00000101
B = 00000011

A + B

  00000101
+ 00000011
----------
  00001000

The calculation result is:

5+3=8

The corresponding binary is:

00001000

5. From Programs to Binary Operations

Take a simple C program as an example:

#include <stdio.h>

int main(void) {
    int a = 5;
    int b = 3;
    int result = a + b;

    printf("%d\n", result);

    return 0;
}

Programmers write in a high-level language, but the CPU cannot directly execute C source code.

A program usually needs to go through:

  1. Compilation.
  2. Assembly.
  3. Linking.
  4. Loading.
  5. CPU execution of machine instructions.

Machine instructions are represented by binary encoding, and the CPU identifies the operation type, operands, and other control information based on the instruction encoding.