Binary Math Explained- Understanding Computer Numbers
What the Hell Is Binary, Anyway?
Binary is a number system. That's it. Just a different way of counting.
Humans use decimal (base-10). You have 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. When you hit 9 and need to go higher, you reset to 0 and carry a 1 to the next column.
Computers use binary (base-2). Only two digits: 0 and 1. Every single thing your computer does — every pixel, every sound, every calculation — comes down to these two states: on or off.
That "on" state? A tiny electrical charge held in a transistor. The "off" state? No charge. That's all your CPU is — billions of microscopic switches flipping on and off billions of times per second.
Why Binary and Not Something Else?
Practical reasons. Hardware is easier to build when you only need to distinguish between two states. Noise, interference, component degradation — none of it matters much when you're just detecting "is there voltage or not?"
You could build a ternary computer (base-3), but the engineering headaches aren't worth it. Binary won because it's dead simple to implement reliably at scale.
How Binary Numbers Actually Work
Each position in a binary number represents a power of 2, reading right to left:
- Rightmost bit: 2⁰ = 1
- Next: 2¹ = 2
- Next: 2² = 4
- Next: 2³ = 8
- And so on...
Take the binary number 1011:
Reading right to left: 1×2⁰ + 1×2¹ + 0×2² + 1×2³ = 1 + 2 + 0 + 8 = 11 in decimal.
That's the whole mechanic. Memorize it.
Converting Decimal to Binary: The Quick Method
Divide by 2, keep the remainders, read them backwards.
Let's convert 23 to binary:
- 23 ÷ 2 = 11 remainder 1
- 11 ÷ 2 = 5 remainder 1
- 5 ÷ 2 = 2 remainder 1
- 2 ÷ 2 = 1 remainder 0
- 1 ÷ 2 = 0 remainder 1
Read the remainders bottom to top: 10111
Check: 1×16 + 0×8 + 1×4 + 1×2 + 1×1 = 16 + 0 + 4 + 2 + 1 = 23 ✓
Binary Addition
Same as decimal, but with only two digits. Rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 0 (carry 1)
- 1 + 1 + 1 = 1 (carry 1)
Example: 101 + 011
1 0 1
+ 0 1 1
------
1 0 0 0
Working right to left: 1+1=0 carry 1, then 0+1+carry1=0 carry 1, then 1+0+carry1=0 carry 1, then just the carry = 1.
Result: 1000 (which is 8 in decimal, and 5+3=8 ✓)
Binary Subtraction Using Two's Complement
Skip the borrowing method. It's a pain. Modern computers handle subtraction by converting to two's complement.
To get two's complement:
- Flip all bits (0→1, 1→0)
- Add 1
Then just add.
Example: 5 - 3
5 in binary: 0101
3 in binary: 0011
Flip 0011 → 1100, add 1 → 1101 (this is -3)
Now add 0101 + 1101:
0 1 0 1
+ 1 1 0 1
---------
1 0 1 0
Result: 0010 (ignoring the overflow bit) = 2 ✓
Bits, Bytes, and Why the Numbers Look Weird
Your computer thinks in chunks:
- Bit: single binary digit (0 or 1)
- Byte: 8 bits (can represent 0-255)
- Word: typically 32 or 64 bits depending on your system
That's why 1KB = 1024 bytes, not 1000. Computers use powers of 2. 2¹⁰ = 1024 ≈ 1000, close enough for naming conventions.
When you see 256, 512, 1024, 2048 — these are all powers of 2. That's your computer's native vocabulary.
Hexadecimal: The Shortcut Programmers Use
Binary is verbose. Writing out 16 bits for every small number is exhausting. Hexadecimal (base-16) solves this.
Hex uses 16 symbols: 0-9, then A-F (where A=10, B=11, C=12, D=13, E=14, F=15).
Each hex digit = exactly 4 binary digits.
Example conversions:
| Decimal | Binary | Hexadecimal |
|---|---|---|
| 0 | 0000 | 0 |
| 10 | 1010 | A |
| 15 | 1111 | F |
| 255 | 11111111 | FF |
This is why memory addresses look like 0x7FFF instead of their binary equivalents. Hex is human-readable while staying close to the machine.
How Negative Numbers Actually Work
Most systems use signed integers with two's complement representation. The leftmost bit indicates sign:
- 0 in the MSB = positive
- 1 in the MSB = negative
With 8 bits, you get -128 to +127 instead of 0 to 255.
Why two's complement for negatives? Because it makes arithmetic work automatically. The same addition circuit handles both signed and unsigned numbers. No special subtraction hardware needed.
That's the entire reason it's used. Simplicity in hardware.
Getting Started: Your First Binary Calculations
Task 1: Convert 42 to binary
42 ÷ 2 = 21 r0
21 ÷ 2 = 10 r1
10 ÷ 2 = 5 r0
5 ÷ 2 = 2 r1
2 ÷ 2 = 1 r0
1 ÷ 2 = 0 r1
Answer: 101010
Task 2: Add 1101 (13) and 1011 (11)
1 1 0 1
+ 1 0 1 1
----------
1 1 0 0 0
Answer: 11000 = 24 ✓
Task 3: Find two's complement of 5 (8-bit)
5 = 00000101
Flip: 11111010
Add 1: 11111011 = -5
Floating Point: Where Things Get Ugly
Integers are clean. Real numbers (with decimals) are messy.
Floating point uses scientific notation in binary: sign bit, exponent, mantissa.
The infamous problem: 0.1 + 0.2 ≠ 0.3 in most programming languages.
0.1 can't be represented exactly in binary floating point. It's a repeating fraction, like trying to write 1/3 in decimal. You get truncation. Always.
This isn't a bug. It's a fundamental limitation of the representation. Financial software uses fixed-point arithmetic for this reason.
What You Should Actually Retain
- Binary is base-2. Two digits. On or off.
- Each position = power of 2
- Divide by 2 + track remainders = decimal to binary
- Two's complement = how negatives are stored
- Hex exists because 4 bits = 1 hex digit, and it's easier to read
- Floating point is imprecise by design
You don't need to manually convert every number you encounter. That's what calculators and programming languages are for. But understanding why these representations work the way they do — that's what separates people who can debug weird edge cases from those who can't.