Binary: Why is the Computer a Fool knowing only 0 and 1?
1. "Computers have no flexibility"
To a computer, the world has only two colors: 0 (OFF) or 1 (ON). Why not Decimal? Humans have 10 fingers. Actually, early computer pioneers like Babbage did try to make Decimal computers using gears. But electronic computers chose Binary.
2. War with Noise: Voltage Dances
Suppose we build a Decimal Computer with 5 Volts.
- 0V = Number 0
- 0.5V = Number 1
- ...
- 5V = Number 9
Since electricity in the real world is not clean, noise makes voltage dance. If I sent 5V (9) but 4.6V arrived due to resistance, is it 8 or 9? This ambiguity causes fatal errors in calculation.
Binary's Strong Immunity
"Let's not split it complexly, just go with Is it there or not (On/Off)!"
- 0V ~ 1.5V: "Low" -> 0
- 3.5V ~ 5V: "High" -> 1
This wide Threshold makes digital systems incredibly reliable. Even if noise corrupts 5V to 3.8V, it is still safely in the "High" zone.
3. Bit, Byte, and Encoding
We group 8 bits into a Byte. ($2^8 = 256$ possibilities).
ASCII vs Unicode
- ASCII: 7-bit standard. Covers English alphabet and numbers.
- Unicode: The universal standard. Maps every character in human languages to a unique code point (U+1F600).
- UTF-8: The most popular encoding for Unicode. It uses 1 to 4 bytes. It's backward compatible with ASCII.
4. Advanced: Negative Numbers & Two's Complement
To store -5:
We use Two's Complement.
- Invert bits (
0000 0101->1111 1010) - Add 1 (
1111 1011) This allows5 + (-5) = 0using standard binary addition logic. No special subtraction hardware needed.
5. Advanced: Floating Point (IEEE 754)
0.1 in binary is an infinite repeating fraction 0.000110011....
Computers truncate it, causing precision errors like 0.1 + 0.2 != 0.3.
IEEE 754 defines how to store these numbers using Sign, Exponent, and Mantissa bits. This is why you should never use float types for financial calculations.
- Sign (1 bit): Positive or Negative.
- Exponent (8 bits): The power of 2 (The moving range of radix point).
- Mantissa (23 bits): The significant digits.
Because we only have 23 bits for Mantissa, we lose precision after certain decimal points. This is an inherent limitation of binary floating point systems not a bug.
6. Binary in Practice: Bitwise Operators
You might think "I'm a high-level developer, I don't need bits." But understanding bitwise logic is crucial for performance and understanding low-level systems.
6.1. Bitmasking (The Art of Efficiency)
Look at Unix permissions chmod 755 (rwxr-xr-x).
Instead of creating 3 boolean columns (CanRead, CanWrite, CanExec), we use a single integer.
- Read (4):
100 - Write (2):
010 - Execute (1):
001
If I want Read + Execute: 4 | 1 = 5 (101).
// Adding permission (OR)
let permission = 0;
permission = permission | READ | WRITE; // 110 (6)
// Checking permission (AND)
if (permission & WRITE) {
console.log("Written!");
}
// Removing permission (NOT + AND)
permission = permission & ~WRITE; // 100 (4)
6.2. Shifting (<<, >>)
Shifting bits to the left is multiplying by 2.
Shifting bits to the right is dividing by 2.
5 << 1 becomes 10. 10 >> 1 becomes 5.
This is much faster for the CPU than actual multiplication instructions.
7. Advanced: Hexadecimal and Bases
Why do we use Hexadecimal (Base-16)?
Because Binary is too long for humans to read.
1011 0101 1100 1111 is hard to read.
Group them by 4 bits: B (11) 5 (5) C (12) F (15).
0xB5CF. Much cleaner.
This maps perfectly to bytes. 1 Byte (8 bits) is exactly 2 Hex digits.
That's why colors are #FFFFFF (Red FF, Green FF, Blue FF).
8. Why is Base64 called Base64?
Why do we convert images or files to Base64 strings? Because some systems (like email or JSON) are designed to handle text, not binary data.
Base64 increases the size by 33%. Why? It takes 3 bytes (24 bits) of binary data and splits it into 4 chunks of 6 bits. ($2^6 = 64$). Each 6-bit chunk maps to a printable character (A-Z, a-z, 0-9, +, /).
- Input:
Man(ASCII) ->010011010110000101101110(Total 24 bits) - Split:
010011(19)010110(22)000101(5)101110(46) - Map:
TWFu - Output:
TWFu
So when you see data:image/png;base64,..., you are looking at raw binary data chopped into 6-bit pieces to survive in a text-only world. It's a bridge between the binary underworld and the text-based internet.
9. 1KB vs 1KiB: The Great Confusion
Why does my 512GB SSD only show 476GB in Windows? It's because hard drive manufacturers and Windows speak different languages.
- Decimal (SI Units): Used by manufacturers.
- 1 KB = 1,000 Bytes ($10^3$)
- 1 MB = 1,000,000 Bytes
- Binary (IEC Units): Used by Windows/Computers.
- 1 KiB (Kibibyte) = 1,024 Bytes ($2^$)
- 1 MiB (Mebibyte) = 1,048,576 Bytes
Manufacturers say: "Here is $512 \times 10^9$ bytes!" Windows says: "Okay, dividing by $2^$... That's 476 GiB." So you are not being cheated. It's just a unit mismatch born from the binary nature of computers.
10. Warning: Integer Overflow
What happens if you add 1 to the maximum integer?
In an 8-bit system, the max is 1111 1111 (255).
Adding 1 result in 1 0000 0000 (256).
But the system only has 8 bits, so the leading 1 is dropped (Overflow).
The result becomes 0000 0000 (0).
This is why:
- Pac-Man Level 256 Glitch: The game tried to draw 256 fruit but the counter wrapped around to 0, crashing the level renderer.
- Gandhi in Civilization: His aggression was 1. A peaceful action reduced it by 2. It wrapped around to 255 (Max Aggression), turning him into a nuclear warmonger.
- Y2K38 Problem: Unix time counts seconds since Jan 1, 1970 using a 32-bit signed integer. It will overflow on January 19, 2038, wrapping around to 1901. We must migrate to 64-bit systems before then.
11. Future: Quantum Computing (Qubit)
We live in a world of Bits (0 OR 1). The future belongs to Qubits (0 AND 1). Thanks to Superposition, a Qubit can exist in both states simultaneously. While a classic computer tries every path in a maze one by one, a quantum computer tries all paths at once. However, for now, mastering 0 and 1 is enough to build great software.
12. Summary
- Reliability: Binary is chosen to fight Electrical Noise.
- Byte: 8 bits. The atom of digital data.
- Two's Complement: Clever math for negative numbers.
- Floating Point: Precision trade-offs.
- Hexadecimal: The lens for viewing binary.
- Base64: Binary disguised as text.
Understanding binary helps you understand data types, memory limits, and character encodings. It is the alphabet of our digital world.