Topic 1 · Data representation
Binary, denary, and hexadecimal
Why computers count in binary, how to convert between binary, denary, and hex, and where hex is used.
Why binary?
A computer is built from billions of tiny switches called transistors. Each switch is either off or on, so the natural way to store anything is with two digits: 0 and 1. A single 0 or 1 is a bit (binary digit).
Every number, letter, sound, and picture a computer handles is stored as patterns of bits. The data has to be converted to binary before the computer can process it.
Key idea. Binary is base 2. Each place is worth double the place to its right: 1, 2, 4, 8, 16, 32, 64, 128.
Place values
Denary (base 10) has place values 1, 10, 100, 1000. Binary works the same way with powers of 2. To read a binary number, add up the place values that have a 1 under them.
Try it below. Click any bit to flip it and watch the denary value change. Can you make 75? (Hint: 64 + 8 + 2 + 1.)
00000000 (0) to 11111111 (255).Denary to binary
Method 1, subtract place values: start from the largest place value that fits, write a 1, subtract, and move right. For 75: 64 fits (11 left), 32 no, 16 no, 8 fits (3 left), 4 no, 2 fits (1 left), 1 fits. So 75 = 01001011.
Method 2, divide by 2: keep dividing by 2 and write down the remainders. Read the remainders from the bottom up.
| Number | ÷ 2 | Remainder |
|---|---|---|
| 75 | 37 | 1 |
| 37 | 18 | 1 |
| 18 | 9 | 0 |
| 9 | 4 | 1 |
| 4 | 2 | 0 |
| 2 | 1 | 0 |
| 1 | 0 | 1 |
1001011. Pad to 8 bits: 01001011.Hexadecimal
Hexadecimal (hex) is base 16. It uses the digits 0–9 and then the letters A–F for 10–15. One hex digit stands for exactly 4 bits (a nibble), so a byte is always two hex digits.
To convert binary to hex, split the bits into groups of four from the right and convert each group. To go back, turn each hex digit into its 4 bits.
1011 0111 → B (11) and 7 → B7. In denary that is 11 × 16 + 7 = 183.Hex to denary and back
Hex place values are powers of 16: 1, 16, 256, 4096. So 1F4 = 1 × 256 + 15 × 16 + 4 × 1 = 500.
To turn denary into hex, the quickest route in an exam is often denary → binary → hex. For 500: 0001 1111 0100 → 1F4.
| Denary | Binary | Hex |
|---|---|---|
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 12 | 1100 | C |
| 15 | 1111 | F |
| 16 | 0001 0000 | 10 |
| 255 | 1111 1111 | FF |
Where hex is used
Hex is shorter than binary and easier for people to read and type, with fewer mistakes. You will see it in:
- HTML colour codes, such as
#FF8800(red FF, green 88, blue 00). - MAC addresses, such as
00-1A-2B-3C-4D-5E. - Error codes and memory dumps, such as
0x0000007B. - IPv6 addresses, such as
2001:0db8::ff00:0042. - URLs, where
%20is the hex code for a space.
Watch out. Hex is a way for humans to write binary. The computer still stores the binary underneath.
Key terms
- Bit
- A single binary digit, 0 or 1.
- Nibble
- 4 bits. One hex digit.
- Byte
- 8 bits. Two hex digits.
- Denary
- Base 10, the number system people use every day.
- Hexadecimal
- Base 16, using 0–9 and A–F.
Check yourself
Convert 10010110 to denary.Show answer
128 + 16 + 4 + 2 = 150.
Convert 200 to 8-bit binary.Show answer
200 = 128 + 64 + 8 → 11001000.
Convert 3C to binary and denary.Show answer
3 → 0011, C → 1100, so 00111100. Denary: 3 × 16 + 12 = 60.
Give two reasons programmers use hex instead of binary.Show answer
It is shorter, so easier to read and remember, and people make fewer mistakes typing it. It also converts to binary directly.