Topic 1 · Data representation

Binary, denary and hexadecimal conversion

Video lesson: Binary, denary and hexadecimal conversion▶ Watch the video lessonShort, exam-focused explanation of this topic on the CS with Aamir YouTube channel.

Computers store all data as binary – patterns of 1s and 0s – because their circuits are built from switches that are either on or off. People find long binary numbers hard to read, so we also use denary (base 10) and hexadecimal (base 16). In the exam you must be able to convert between all three.

Binary place values

Each binary digit (bit) has a place value that doubles from right to left. For an 8-bit binary number (one byte) the place values are:

1286432168421

Binary to denary

Write the binary number under the place values and add up every place value that has a 1 under it.

Example: 11001000 → 128 + 64 + 8 = 200

Denary to binary

Start at the largest place value (128). If it fits into the number, write 1 and subtract it; if not, write 0. Move to the next place value and repeat until you reach 1.

Example: 75 → 64 fits (1, remainder 11) · 32 no (0) · 16 no (0) · 8 fits (1, remainder 3) · 4 no (0) · 2 fits (1, remainder 1) · 1 fits (1). With 0 for 128 at the front: 01001011

Hexadecimal digits

Hexadecimal uses 16 digits: 0–9, then A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15. One hex digit represents exactly 4 bits (a nibble), which is why conversion between binary and hex is so quick.

Binary to hexadecimal

Split the binary number into groups of 4 bits starting from the right, then convert each group to one hex digit.

Example: 11001000 → 1100 | 1000 → 12 | 8 → C8

Hexadecimal to binary

Convert each hex digit into its 4-bit binary pattern and join them together.

Example: 3F → 0011 | 1111 → 00111111

Hexadecimal to denary

The place values in hexadecimal are 256, 16 and 1. Multiply each digit by its place value and add.

Example: 3F → (3 × 16) + (15 × 1) = 48 + 15 = 63

To go from denary to hex, you can convert the number to binary first and then group into nibbles, or divide by 16: 200 ÷ 16 = 12 remainder 8 → C8.

Why do we use hexadecimal?

Common uses you should be able to name include HTML colour codes (e.g. #FF0000 for red), MAC addresses, IPv6 addresses and error codes.

Exam tips

Want the full topic notes, slides and worksheets? See Notes & Slides, practise with the free CS with Aamir Quiz App, or order the O Level Computer Science Study Guide.