Why Binary, and How Positional Systems Work
Quick answer A computer stores numbers in base 2 because a transistor reliably has only two states, and every base - 2, 8, 10 or 16 - works by the same rule: each digit is worth the digit times the base raised to its place number.
A processor has no idea what a 5 is. Inside the chip there are only transistors, and a transistor is dependably in one of two conditions: current flowing or not flowing, high voltage or low voltage. Two states can be told apart even when the chip is hot, the power supply sags a little, and there is electrical noise on the line. Ten different voltage levels cannot be told apart that reliably. That one engineering fact is the whole reason your Aadhaar number, a UPI payment of two hundred rupees, and this sentence are all finally stored in base 2.
One two-state cell is a bit. Eight bits grouped together are a byte. With n bits you can build 2n different patterns, so a byte has 28 = 256 patterns, which hold the values 0 to 255. This is not a rule someone invented; it is just counting. Each extra bit doubles the number of patterns because that bit can be 0 or 1 on top of everything the earlier bits could already do.
Positional notation. Decimal already works the way binary does, you have simply never had to think about it. In 4507 the 5 is not "five", it is "five hundred", and the only reason is where it sits. Every positional system has a base (also called the radix) and a set of legal digits running from 0 up to base minus 1. The value of the whole number is:
value = d(k) x b^k + ... + d(2) x b^2 + d(1) x b^1 + d(0) x b^0
Place numbers start at 0 at the rightmost digit and count leftwards. So (4507)10 = 4x1000 + 5x100 + 0x10 + 7x1. Change the base and nothing about the method changes - only the number you raise to the power.
| System | Base | Legal digits | Python prefix | Example |
|---|---|---|---|---|
| Binary | 2 | 0 1 | 0b | 0b1010 |
| Octal | 8 | 0 1 2 3 4 5 6 7 | 0o | 0o12 |
| Decimal | 10 | 0 to 9 | none | 10 |
| Hexadecimal | 16 | 0 to 9, then A B C D E F | 0x | 0xA |
Base 16 runs out of ordinary digits after 9, so the letters A to F are pressed into service for the values 10 to 15. A is not a letter here, it is the number ten.
Why octal and hexadecimal exist at all. Machines are happy with binary; humans are not. Writing 2026 as 11111101010 and copying it without dropping a bit is painful. But 8 = 23 and 16 = 24, so exactly three bits collapse into one octal digit and exactly four bits collapse into one hex digit, with no arithmetic and no carrying. Decimal gets no such shortcut, because 10 is not a power of 2. That is why memory addresses, MAC addresses and web colour codes are written in hex - it is compressed binary that a human can read aloud.
Worked example. The three ideas above, checked by running them:
# A decimal number is really a sum: digit x base ** place
print(4 * 10**3 + 5 * 10**2 + 0 * 10**1 + 7 * 10**0)
# One byte = 8 bits. How many patterns does that give?
print(2**8, "patterns, values 0 to", 2**8 - 1)
# Python reads all four bases directly. All four below are the same number.
print(0b1010, 0o12, 10, 0xA)
Output:
4507
256 patterns, values 0 to 255
10 10 10 10
The last line is the point worth remembering: 0b1010, 0o12, 10 and 0xA are four ways of typing one single number. The prefix affects nothing inside the machine - once stored, all four are the same pattern of bits, and Python prints them the same way.
Second worked example. A web colour such as #FF9933 is nothing but three two-digit hex numbers stuck together - red, green, blue:
# A web colour like #FF9933 is just three hex numbers: red, green, blue
print(int("FF", 16), int("99", 16), int("33", 16))
print("largest value one pair of hex digits can hold:", int("FF", 16))
Output:
255 153 51
largest value one pair of hex digits can hold: 255
Two hex digits cover exactly 0 to 255, which is exactly one byte. That is not a coincidence - 2 hex digits are 8 bits.
- Computers use base 2 because a transistor can hold two states reliably; ten voltage levels cannot be distinguished dependably, so base 10 hardware is impractical.
- Every positional system follows one rule: value = sum of (digit x base ** place), with places numbered from 0 at the right.
- A base b uses digits 0 to b-1; hexadecimal borrows A to F for the values 10 to 15.
- n bits give 2**n patterns, holding values 0 to 2**n - 1; a byte therefore holds 0 to 255.
- Octal and hex exist for human convenience, because 8 = 2**3 and 16 = 2**4 let bits be grouped exactly; decimal has no such shortcut.
