Binary, denary and hexadecimal
Computers store everything as binary, but people find long strings of 1s and 0s hard to read. This topic is about moving between the three number systems in the syllabus, and about what happens to 8-bit values when you add, shift or make them negative.
Converting between the three systems
Denary is base 10, binary is base 2 and hexadecimal is base 16, using 0–9 then A–F for 10–15. For 8-bit binary, write the place values 128, 64, 32, 16, 8, 4, 2, 1 above the bits and add up the columns holding a 1. To go the other way, work from the left, putting a 1 in each column whose value still fits into what is left.
Hexadecimal maps neatly onto binary because one hex digit is exactly four bits (a nibble). Split a byte into two nibbles and convert each on its own: 1011 0110 is B then 6, so B6. Going from hex to denary, the left digit is worth 16s: B6 is 11 × 16 + 6 = 182.
Why hexadecimal is used
- It is shorter than binary, so easier to read and write.
- People make fewer mistakes copying it than copying long binary strings.
- Each digit converts directly to four bits, so it is easy to turn back into binary.
- Typical uses: colour codes in HTML, MAC addresses, IPv6 addresses, error codes and memory dumps.
Binary addition and overflow
Add column by column from the right: 1 + 1 is 0 carry 1, and 1 + 1 + 1 is 1 carry 1. In an 8-bit register the largest value is 255 (11111111). If a sum needs a ninth bit, the result does not fit and an overflow error occurs. The value that is left in the register is wrong.
Logical shifts
A logical shift moves every bit left or right by a number of places. Bits that fall off the end are lost, and the empty places are filled with 0s. Each place shifted left multiplies an unsigned value by 2; each place shifted right divides it by 2, dropping any remainder. When a 1 falls off the left, the multiplied result is no longer correct.
Two’s complement
Two’s complement stores negative numbers by making the leftmost bit worth −128 instead of 128, so 8 bits cover −128 to 127. To write a negative number, write the positive version in binary, flip every bit, then add 1. For example, 5 is 00000101, flipped is 11111010, and adding 1 gives 11111011, which is −5. Check it: −128 + 64 + 32 + 16 + 8 + 2 + 1 = −5.
Where marks go
- Writing fewer than 8 bits when the question asks for an 8-bit value, e.g. 101 instead of 00000101.
- Converting hex to denary digit by digit and writing the digits side by side (B6 as “116”) instead of multiplying the left digit by 16.
- Saying overflow happens because “the number is too big” without saying it needs more bits than the register holds.
- Forgetting that bits shifted off the end are lost, so the shifted value can be wrong.
- Flipping the bits for two’s complement but not adding 1.