
How Binary Works — Ones, Zeros, and Everything Between
A computer does not understand words, decimals, or images. It understands voltage — high or low, on or off, 1 or 0. Every number, every character, every instruction, every pixel is encoded as a sequence of ones and zeros. This is binary.
What Is a Bit?
A bit (binary digit) is the smallest unit of information: a single 0 or 1. One bit can represent two states — true/false, yes/no, on/off. Two bits can represent four states (00, 01, 10, 11). Three bits can represent eight states. The pattern: n bits can represent 2^n distinct values.
| Bits | Distinct values | Range (unsigned) |
|---|---|---|
| 1 | 2 | 0–1 |
| 4 | 16 | 0–15 |
| 8 | 256 | 0–255 |
| 16 | 65,536 | 0–65,535 |
| 32 | 4,294,967,296 | 0–4.29 billion |
| 64 | 18.4 × 10^18 | 0–18.4 quintillion |
What Is a Byte?
A byte is 8 bits. It's the fundamental unit of memory — the smallest addressable unit on most computers. One byte can represent 256 values (0–255).
Memory sizes are measured in bytes:
- 1 KB (kilobyte) = 1,024 bytes
- 1 MB (megabyte) = 1,024 KB = 1,048,576 bytes
- 1 GB (gigabyte) = 1,024 MB ≈ 1 billion bytes
- 1 TB (terabyte) = 1,024 GB ≈ 1 trillion bytes
When you hear "a 64-bit processor," it means the CPU works with 64 bits at a time — it can process numbers up to 2^64 in a single operation and address up to 16 exabytes of memory.
How Do You Count in Binary?
Decimal uses ten digits (0–9) and powers of 10. Binary uses two digits (0–1) and powers of 2.
| Binary | Calculation | Decimal |
|---|---|---|
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 2+1 | 3 |
| 0100 | 4 | 4 |
| 0101 | 4+1 | 5 |
| 1000 | 8 | 8 |
| 1010 | 8+2 | 10 |
| 1111 | 8+4+2+1 | 15 |
| 11111111 | 128+64+32+16+8+4+2+1 | 255 |
Each position is a power of 2, right to left: 2^0=1, 2^1=2, 2^2=4, 2^3=8, and so on. To convert binary to decimal, add up the powers of 2 where there's a 1.
What Is Hexadecimal?
Binary is hard to read — 11000000101010000000000100000001 is an IP address, but you'd never recognize it. Hexadecimal (hex, base 16) is a compact way to write binary. Each hex digit represents exactly 4 bits:
| Hex | Binary | Decimal |
|---|---|---|
| 0 | 0000 | 0 |
| 5 | 0101 | 5 |
| 9 | 1001 | 9 |
| A | 1010 | 10 |
| F | 1111 | 15 |
| FF | 11111111 | 255 |
| C0A80101 | 11000000 10101000 00000001 00000001 | 192.168.1.1 |
One byte = two hex digits. Memory addresses, color codes (#FF0000 = red), MAC addresses (AA:BB:CC:DD:EE:FF), and binary dumps are all written in hex. The 0x prefix marks a hex number: 0xFF = 255.
How Are Negative Numbers Represented?
If 8 bits represent 0–255, how do you represent -1? The most common scheme is two's complement:
- The highest bit is the sign bit: 0 = positive, 1 = negative.
- To negate a number: flip all bits and add 1.
| Binary | Unsigned | Signed (two's complement) |
|---|---|---|
| 00000001 | 1 | 1 |
| 01111111 | 127 | 127 |
| 10000000 | 128 | -128 |
| 11111111 | 255 | -1 |
| 11111110 | 254 | -2 |
With two's complement, 8 bits represent -128 to 127. 32 bits represent -2.1 billion to 2.1 billion. The beauty is that addition works the same for signed and unsigned — the CPU doesn't need separate circuits.
How Are Decimals Represented?
Integers are exact in binary. Decimals are not. Floating point (IEEE 754) represents numbers like 3.14 by splitting the bits into three fields: sign, exponent, and mantissa (significand).
A 64-bit double has:
- 1 sign bit
- 11 exponent bits (range)
- 52 mantissa bits (precision)
This gives approximately 15-17 significant decimal digits. But not all decimals can be represented exactly — 0.1 + 0.2 = 0.30000000000000004 in every programming language. This isn't a bug; it's a consequence of representing base-10 fractions in base-2.
This is why money should never use floating point. Use integers (store cents, not dollars) or decimal types designed for exact arithmetic.
Why Does Binary Matter?
Binary is not just an academic concept. It shows up constantly:
Permissions — Unix file permissions are 3 octal digits. chmod 755 = 111 101 101 = rwx r-x r-x. Each bit is a permission.
Networking — IP addresses are 32-bit numbers. Subnet masks are binary patterns. TCP flags (SYN, ACK, FIN, RST) are individual bits in a byte.
Colors — #FF8800 = 255 red, 136 green, 0 blue. Each color channel is one byte.
Bitwise operations — checking if a number is even (n & 1 == 0), setting flags, extracting fields from packed data. Hash maps use bit masking for bucket selection.
Overflow — a 32-bit unsigned integer wraps from 4,294,967,295 to 0 on increment. A signed integer wraps from 2,147,483,647 to -2,147,483,648. This has caused real bugs — the Y2K problem, the 2038 problem (32-bit Unix timestamps), Gangnam Style breaking YouTube's view counter.
Next Steps
- How Boolean Logic Works — the AND/OR/NOT operations built from bits.
- How Memory Works — how bytes are organized into stack, heap, and virtual memory.