In this chapter
We'll learn to count like a computer: the decimal, binary, octal and hexadecimal number systems, how to convert between them by hand, and why computers use binary in the first place.
The Problem in Real Life
John opens Zoë's name in a tool called a hex viewer, which shows the raw bytes of a file. Instead of letters, Anna sees this: 5A 6F C3 AB.
"Four bytes," John says. "Z, o, and two bytes for the ë. But to understand why the ë broke, you need to be able to read these numbers." Anna stares at the screen. Since when do numbers have letters in them?
C3? AB? I thought numbers only went up to 9.
Anna
Counting in Tens vs. Counting in Twos
Numbers with letters
Hexadecimal uses A–F as digits, which looks strange until you see why.
Same number, different ways to write it
Thirteen can be written 13, 1101 or D — it's the same amount, written in different number systems.
Number Systems and How to Convert Between Them
John asks a strange question: "Why do we count in tens?" Anna shrugs. "Because... we have ten fingers?" "Exactly. It's not special. It's just the system we picked."
A number system is a way of writing amounts using a fixed set of digits. The number of digits it uses is called its base. When you run out of digits, you carry over to the next column — the same way 9 + 1 becomes 10.
- Decimal (base 10) — digits 0 to 9. What people use every day. Each column is worth 10 times the one on its right: ones, tens, hundreds. So 347 = 3 hundreds + 4 tens + 7 ones.
- Binary (base 2) — digits 0 and 1 only. What computers use. Each column is worth 2 times the one on its right: 1, 2, 4, 8, 16, 32, 64, 128...
- Octal (base 8) — digits 0 to 7. Not used much today, but you'll meet it in Linux file permissions like
755(Act 06). - Hexadecimal (base 16), or hex — digits 0 to 9, then A, B, C, D, E, F for ten to fifteen. Programmers love hex because one hex digit is exactly 4 bits, so one byte (8 bits) is always exactly two hex digits.
C3is just a short way to write the byte11000011.
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 5 | 0101 | 5 | 5 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 13 | 1101 | 15 | D |
| 15 | 1111 | 17 | F |
| 16 | 1 0000 | 20 | 10 |
| 255 | 1111 1111 | 377 | FF |
255 is the biggest value one byte can hold: eight 1s in binary, FF in hex.
| Column value | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| Bit | 1 | 1 | 0 | 1 |
| Counts? | 8 | 4 | — | 1 |
Add the columns that have a 1: 8 + 4 + 1 = 13.
| Place | Example | What it means |
|---|---|---|
| Web colours | #FF0000 | Red 255, green 0, blue 0 — pure red |
| Raw file bytes | C3 AB | Two bytes of a file, shown in a hex viewer |
| Network hardware | 3C:22:FB:9A:10:7E | A MAC address (Act 11) |
| Error codes | 0x80070005 | A Windows error number ("0x" means hex) |
Why do computers use binary at all? Because inside a chip, everything is electricity, and the easiest thing to build reliably is a switch with two states: current flowing (1) or not flowing (0). A tiny electronic switch called a transistor does this, and a modern CPU has billions of them. Trying to tell ten different voltage levels apart would be far more error-prone than telling just two apart. Two states is simple, cheap and very reliable.
Binary → decimal. Write the column values over the bits and add up the columns that have a 1. For 1101: the columns are 8, 4, 2, 1. So 8 + 4 + 0 + 1 = 13.
Decimal → binary. Keep dividing by 2 and write down the remainders, then read them from bottom to top. 13 ÷ 2 = 6 remainder 1; 6 ÷ 2 = 3 remainder 0; 3 ÷ 2 = 1 remainder 1; 1 ÷ 2 = 0 remainder 1. Read upwards: 1101.
Binary → hex. Split the bits into groups of four from the right, and turn each group into one hex digit. 1100 0011 → 1100 is 12, which is C; 0011 is 3. So 11000011 = C3.
Hex → binary. Do the opposite: turn each hex digit into four bits. AB → A is 10 = 1010, B is 11 = 1011. So AB = 10101011.
Now Anna can read the hex viewer: 5A 6F C3 AB is four bytes. She doesn't yet know which letters they stand for — that's the chapter after next — but they're no longer a mystery. They're just numbers, written in a short form.
Key Takeaway
Decimal, binary, octal and hex are just different ways to write the same amounts. Computers use binary because a switch with two states is simple and reliable; programmers use hex because one hex digit is exactly four bits, so every byte fits in two hex digits.
Why This Matters
You won't convert numbers by hand every day at work. But you will constantly see binary and hex: in colour codes, error messages, memory addresses, network addresses and debugging tools. Being able to read them calmly — instead of panicking at "0x80070005" — is a real everyday skill, and it's exactly what lets Anna solve Zoë's bug two chapters from now.
Anna can now read bytes. Before she gets to Zoë's name, John shows her something that surprises almost every new programmer: numbers themselves can go wrong inside a computer — and on a ticketing site, those numbers are prices.
