Guide
Binary and hexadecimal
Why computers count in twos, why programmers write in sixteens, and how to read a hex colour or a memory address without converting anything in your head.
Counting with only two digits
Decimal has ten digits and each column is worth ten times the one to its right. Binary has two digits and each column is worth twice the one to its right. That is the entire difference; everything else follows from it.
So 1011 in binary is one eight, no fours, one two and one one — eleven. Reading binary is adding up the columns that have a 1 in them, and the columns are the powers of two: 1, 2, 4, 8, 16, 32 and onwards.
The reason to bother is physical. A circuit can distinguish current from no current reliably and cheaply; distinguishing ten voltage levels reliably is much harder. Binary is not a mathematical preference, it is what the hardware can do without making mistakes.
Why hexadecimal exists
Binary is correct and unreadable. Thirty-two bits written out is a wall of ones and zeros that no one can check by eye, and a single transposed digit is invisible.
Hexadecimal has sixteen digits — 0 to 9 then A to F — and sixteen is two to the fourth. That is the whole trick: **one hex digit is exactly four bits**, always, with no arithmetic. You can convert by looking, four bits at a time, in either direction.
This is why a colour is written #FF8800 rather than as twenty-four bits, and why memory addresses and hashes are written in hex. Nobody is doing base-sixteen arithmetic; they are reading binary in a form that fits on a line.
One number, three notations
| Number | In binary | In hexadecimal |
|---|---|---|
| 255 | 1111 1111 · FF | Eight bits all set. FF is the largest two-digit hex number, which is why full intensity in a web colour is written that way. |
| 4,095 | 1111 1111 1111 · FFF | Twelve bits, three hex digits. The full-scale reading of a common analogue-to-digital converter. |
| 16,777,215 | 24 ones · FFFFFF | Six hex digits, which is exactly the form a CSS colour takes: two each for red, green and blue. |
The value never changes — only how many symbols it takes to write. Hex is shortest precisely because each of its digits carries four bits' worth of information.
Reading a hex colour
A colour like #FF8800 is three pairs: FF is red, 88 is green, 00 is blue. Each pair is one byte, so each runs 00 to FF — nothing to 255 — and the three together give the 16,777,216 colours of true colour.
Once you know FF is 255 and 80 is about half, you can read a colour approximately without converting it. #FF0000 is pure red; #808080 is mid grey; #FFFFFF is white because every channel is at maximum.
The same reading applies anywhere bytes are shown in hex. A pair of hex digits is one byte, and a byte is a number from 0 to 255, whatever it happens to represent.