Binary, denary and hexadecimal
Computers store everything as bits that are either 0 or 1. You need to convert between binary, denary (base 10) and hexadecimal (base 16), add binary numbers, and say why programmers like hex.
Part 1 of 3: Learn it
In short
- 8-bit place values: 128, 64, 32, 16, 8, 4, 2, 1.
- One hex digit is exactly four bits (a nibble), so split binary into fours to convert.
- n bits can store 2ⁿ different values: 8 bits give 256 values, 0 to 255.
Where this is in your specification
Spec points: AQA 3.3.1 to 3.3.4, OCR 1.2.1 and 1.2.4, Edexcel Topic 2
| Board | Topic: Data representation |
|---|---|
| AQA 8525 | 3.3.1 to 3.3.8 |
| Edexcel 1CP2 | Topic 2 |
| OCR J277 | 1.2.3 and 1.2.4 (units, numbers, characters, images, sound, compression) |
Binary to denary and back
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
Add the place values with a 1 under them: 1011 0010 = 128 + 32 + 16 + 2 = 178.
To go from denary to binary, take the biggest place value that fits, write a 1 there, subtract it, and repeat with what is left. Write 0 in every column you skip.
Hexadecimal
Hex uses the digits 0 to 9 and then A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Its place values are 1, 16, 256.
- Hex to denary: 3C = 3 × 16 + 12 = 60.
- Denary to hex: 200 ÷ 16 = 12 remainder 8, so C8.
- Binary to hex: split into nibbles from the right and convert each one. 1011 0010 = B2.
- Hex to binary: write each hex digit as four bits. 7E = 0111 1110.
Hex is used because it is shorter than binary, easier for people to read and copy without errors, and converts to binary directly. The computer still stores binary. You will see hex in colour codes, memory addresses and MAC addresses.
Binary addition and overflow
Work from the right-hand column. The rules: 0 + 0 gives 0, 0 + 1 gives 1, 1 + 1 gives 0 with 1 carried, and 1 + 1 + 1 gives 1 with 1 carried. If the answer needs more bits than the register has, the extra carry is lost: that is an overflow error.
Shifts
A left shift of one place multiplies an unsigned binary number by 2; a right shift divides by 2 (losing any remainder). Shifting left by 2 places multiplies by 4. Bits shifted off the end are lost, which can make the result wrong.
Units of data
| Unit | Size |
|---|---|
| bit | a 0 or a 1 |
| nibble | 4 bits |
| byte | 8 bits |
| kilobyte (kB) | 1000 bytes |
| megabyte (MB) | 1000 kB |
| gigabyte (GB) | 1000 MB |
| terabyte (TB) | 1000 GB |
Convert 0010 1101 to denary.
Show the answer
32 + 8 + 4 + 1 = 45.
Part 2 of 3: See it worked
Worked examples
Example 1
Convert 157 to 8-bit binary and then to hexadecimal.
- 157 − 128 = 29 → 1 in the 128 column
- 64 and 32 do not fit → 0, 0
- 29 − 16 = 13 → 1; 13 − 8 = 5 → 1; 5 − 4 = 1 → 1; 2 does not fit → 0; 1 − 1 = 0 → 1
- Binary: 1001 1101. Nibbles: 1001 = 9, 1101 = 13 = D
Answer: 157 = 1001 1101 in binary = 9D in hex.
Example 2
Add 0110 1011 and 0101 0110 as 8-bit numbers. Is there an overflow?
- 0110 1011 = 107 and 0101 0110 = 86
- Adding column by column with carries gives 1100 0001
- 1100 0001 = 128 + 64 + 1 = 193, and 107 + 86 = 193
Answer: 1100 0001 (193). It fits in 8 bits, so there is no overflow.
Common mistakes
- Reading binary from the wrong end. The 1s column is on the right.
- Writing 10 to 15 as two digits in hex instead of A to F.
- Splitting into nibbles from the left when the number of bits is not a multiple of four. Pad with 0s on the left first.
- Saying computers use hex. They store binary; hex is for people.
Convert FF to denary.
Show the answer
15 × 16 + 15 = 255.
Part 3 of 3: Test yourself
Check yourself
Answer each one in your head or on paper first, then open it to check.
Convert 0010 1101 to denary.
32 + 8 + 4 + 1 = 45.
Convert FF to denary.
15 × 16 + 15 = 255.
How many different values can 5 bits represent?
2⁵ = 32.
Jobs that use this
- Software developer (öffnet einen neuen Tab)
- Network engineer (öffnet einen neuen Tab)
- Computer games developer (öffnet einen neuen Tab)
Each link opens the job profile on the National Careers Service (England). In the rest of the UK: My World of Work (Scotland), Careers Wales, nidirect careers (Northern Ireland).
Diese Lernzettel sind auf Englisch, weil sie britischen Prüfungslehrplänen folgen.
Full lessons and marked practice for this course are coming soon to Brainlag Learn. See courses