In this article
You count in tens because you have ten fingers. A computer counts in twos because a switch has two states. Hexadecimal, base 16, sits between them as a tidy way for people to write long strings of bits. None of these systems is harder than decimal. They only change what each position is worth.
Place value: the idea behind every base
In decimal, the number 305 means 3 hundreds, 0 tens and 5 ones. Each place is worth ten times the one on its right. Binary does the same with two. Hex does the same with sixteen.
Every place is double the one to its right. Add up the places that are switched on to get the number. All eight on gives 255.
Reading a binary number
Take 11001010. Write the place values above each bit and add the ones that are on.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 |
| Counts? | 128 | 64 | – | – | 8 | – | 2 | – |
128 + 64 + 8 + 2 = 202.
- On: 128 and 64bits 7 and 6
- Off: 32 and 16bits 5 and 4
- On: 8bit 3
- Off: 4bit 2
- On: 2bit 1
- Off: 1bit 0
Decimal to binary by dividing by 2
To convert, divide by 2 again and again, and write down each remainder. Read the remainders from the last to the first.
- 1202 ÷ 2 = 101Remainder 0. This is the last bit (bit 0).
- 2101 ÷ 2 = 50Remainder 1.
- 350 ÷ 2 = 25 · 25 ÷ 2 = 12Remainders 0 and 1.
- 412 ÷ 2 = 6 · 6 ÷ 2 = 3Remainders 0 and 0.
- 53 ÷ 2 = 1 · 1 ÷ 2 = 0Remainders 1 and 1. Stop when you reach 0.
- 6Read from the bottom up11001010, which is 202.
Why hex exists
Long strings of ones and zeros are hard for people to read and easy to mistype. Hex is a shortcut. Because 16 is 2⁴, one hex digit stands for exactly four bits. You can convert a binary number to hex by splitting it into groups of four from the right.
| Decimal | Binary | Hex | Decimal | Binary | Hex |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 8 | 1000 | 8 |
| 1 | 0001 | 1 | 9 | 1001 | 9 |
| 2 | 0010 | 2 | 10 | 1010 | A |
| 3 | 0011 | 3 | 11 | 1011 | B |
| 4 | 0100 | 4 | 12 | 1100 | C |
| 5 | 0101 | 5 | 13 | 1101 | D |
| 6 | 0110 | 6 | 14 | 1110 | E |
| 7 | 0111 | 7 | 15 | 1111 | F |
Hex uses the letters A to F for the values 10 to 15.
For 11001010, split into 1100 and 1010. 1100 is C (12) and 1010 is A (10). So the hex is CA. Check it: C × 16 + A = 12 × 16 + 10 = 202.
- Decimal, easy to read
- Hard to see which bits are on
- Binary shows every bit
- Hex is a short form: two digits per byte
How big can a number get?
Each extra bit doubles the number of values. n bits can hold 2ⁿ different values, from 0 up to 2ⁿ − 1.
Where you meet these every day
- Web colours.
#6366f1is three bytes: 63, 66 and F1 in hex, which are 99, 102 and 241 in decimal. That is red 99, green 102, blue 241. The contrast checker guide uses these values. - IP addresses. Each part of 192.168.1.10 is one byte (8 bits). See what /24 and /26 mean.
- File permissions. The chmod number 755 is octal (base 8). Each digit is three bits: 7 is 111 (read, write, execute), 5 is 101 (read and execute).
- Characters. The letter A is 65 in decimal, 41 in hex and 01000001 in binary.
- Hashes. A SHA-256 hash is 256 bits, shown as 64 hex digits. See what a hash is.
Quick tips
- Memorise the place values 1, 2, 4, 8, 16, 32, 64, 128. Everything else is addition.
- Learn the sixteen four-bit patterns above. Then hex and binary conversions become instant.
- Write 0x before a hex number in code (0xCA) and 0b before binary (0b11001010) so they are not mistaken for decimal.
- Check by converting back. If you end up at the number you started with, the conversion is right.
Questions people ask
How do I convert binary to decimal?
Write the place values 128, 64, 32, 16, 8, 4, 2, 1 above the bits and add the values where the bit is 1. For 11001010 that is 128 + 64 + 8 + 2 = 202.
How do I convert decimal to binary?
Divide by 2 repeatedly and write down the remainders. Read the remainders from last to first.
Why does hexadecimal use letters?
Base 16 needs sixteen symbols. After 0 to 9, the letters A to F stand for 10 to 15.
Why is one hex digit four bits?
Because 16 = 2⁴. Four bits can hold exactly 16 different values, which one hex digit represents.
What does 0x mean?
In many programming languages, 0x in front of a number means it is written in hexadecimal, for example 0xFF is 255.

