Syllabus coverage
- Understand how and why computers use binary to represent all forms of data, including that data is processed by logic gates and stored in registers
- Understand the denary, binary and hexadecimal number systems as base 10, base 2 and base 16
- Convert between positive denary and positive binary, positive denary and positive hexadecimal, and positive hexadecimal and positive binary, in both directions and up to 16 bits
- Understand how and why hexadecimal is used as a beneficial method of data representation, and identify areas of computer science where it is used
- Add two positive 8-bit binary integers and understand the concept of overflow, including why it occurs in binary addition
- Perform logical left shifts, logical right shifts and multiple shifts on a positive 8-bit binary integer, and understand the effect on its value
- Use the two's complement number system to represent positive and negative 8-bit binary integers, converting in both directions
Part of the O Level Computer Science syllabus 2210.
Why every kind of data becomes binary first
A computer cannot store a letter, a photograph or a decimal point directly. Whatever the data is, it has to be converted into binary before the processor can do anything with it. The reason is the hardware: the circuits are built from transistors, and a transistor behaves like a switch that is either on or off. One switch holds one bit, so a bit is always 0 or 1 and nothing in between.
Once the data is in binary it is handled by logic gates, which combine those 0s and 1s according to fixed rules, and the values being worked on are held in registers inside the processor. A register is simply a row of bits with a fixed width, which is why the width matters so much later in this topic: an 8-bit register can only hold eight bits, and that sets a hard ceiling on the values it can represent.
The numbers you meet in this topic are integers only — no fractions and no decimal points. A question may ask you to work with anything from 4 bits up to 16 bits, so practise writing the place values out rather than trying to hold them in your head.
Denary, binary and hexadecimal side by side
Each number system is named after its base, which is the number of different digits it uses before it has to move into a new column. Denary is base 10 and uses 0 to 9, and it is the system people count in. Binary is base 2 and uses only 0 and 1. Hexadecimal is base 16 and needs sixteen digits, so it uses 0 to 9 and then A to F, where A stands for 10, B for 11, C for 12, D for 13, E for 14 and F for 15.
The base also tells you what each column is worth. In denary the columns are 1, 10, 100, 1000 and so on, each one ten times the one before. In binary the columns double: 1, 2, 4, 8, 16, 32, 64, 128. In hexadecimal the columns are 1, 16, 256, 4096, again each one sixteen times the previous column. Once you can see the columns, a conversion is just deciding which columns to fill.
A single hexadecimal digit covers exactly the same range as four binary digits, because 2 to the power 4 is 16. That one fact is the whole reason hexadecimal exists, and it is worth memorising before anything else on this page.
| System | Base | Digits available | Column values (first four) |
|---|---|---|---|
| Denary | 10 | 0 to 9 | 1, 10, 100, 1000 |
| Binary | 2 | 0 and 1 | 1, 2, 4, 8 |
| Hexadecimal | 16 | 0 to 9 and A to F | 1, 16, 256, 4096 |
Denary to binary and binary to denary
To turn a denary number into binary, write the place values above the columns and take the largest value that fits, subtract it, and repeat with what is left. For 202: 128 fits, leaving 74; 64 fits, leaving 10; 32 and 16 do not fit; 8 fits, leaving 2; 4 does not fit; 2 fits, leaving 0. Fill a 1 under every value you used and a 0 under every value you skipped, giving 1100 1010.
To go the other way, add up the place values of the columns that contain a 1 and ignore the zeros. For 0110 1101 that is 64 + 32 + 8 + 4 + 1 = 109. Writing the place values along the top of the number before you start is the single most reliable habit in this topic, because almost every wrong answer comes from a column being worth the wrong amount.
Sixteen-bit questions work in exactly the same way with a longer row of columns: 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1. The denary value 40 000 is 32768 + 4096 + 2048 + 1024 + 64, which gives 1001 1100 0100 0000. Do not drop the leading zeros when the question specifies a bit width; 1001 1100 0100 0000 is 16 bits and a 12-bit answer is a different question.
A quick check that costs a few seconds: add the place values of your 1s back up and see whether you get the number you started with. If you do not, the error is in the columns, not in the arithmetic.
Binary and hexadecimal in both directions
Because one hex digit is exactly four bits, converting between binary and hexadecimal never involves any arithmetic at all. Split the binary into groups of four, starting from the right-hand end, and replace each group with its single hex digit. Starting from the right matters: grouping from the left puts the nibbles in the wrong places and gives a plausible-looking but wrong answer.
For 1100 1010: the right-hand group 1010 is 10, which is A, and the left-hand group 1100 is 12, which is C, so the answer is CA. Going the other way, replace each hex digit with its own four bits: D3 becomes 1101 for the D and 0011 for the 3, so 1101 0011.
If the binary value is not a multiple of four bits long, pad the left-hand group with leading zeros before converting. A 16-bit value always produces exactly four hex digits, which is why the 16-bit answer 1001 1100 0100 0000 is written 9C40. Learn the sixteen pairings (0000 = 0 up to 1111 = F) and this conversion becomes the fastest marks on the paper.
| Binary | Hexadecimal | Binary | Hexadecimal |
|---|---|---|---|
| 0000 | 0 | 1000 | 8 |
| 0001 | 1 | 1001 | 9 |
| 0010 | 2 | 1010 | A |
| 0011 | 3 | 1011 | B |
| 0100 | 4 | 1100 | C |
| 0101 | 5 | 1101 | D |
| 0110 | 6 | 1110 | E |
| 0111 | 7 | 1111 | F |
Denary to hexadecimal, and why hexadecimal is used
For denary to hexadecimal there are two safe routes. The first is to convert to binary and then group the bits in fours. The second is to divide repeatedly by 16 and read the remainders from the bottom up. For 1500: 1500 ÷ 16 = 93 remainder 12, and 12 is C; 93 ÷ 16 = 5 remainder 13, and 13 is D; 5 ÷ 16 = 0 remainder 5. Reading upwards gives 5DC.
To go from hexadecimal back to denary, multiply each digit by its column value. 5DC is (5 × 256) + (13 × 16) + (12 × 1) = 1280 + 208 + 12 = 1500. The most common error here is using 10 and 100 as the columns out of habit, which is a denary habit applied to a hexadecimal number.
Hexadecimal earns its place because it is a shorter representation of binary that humans can read. Four bits of binary become one character, so a 16-bit value shrinks from sixteen digits to four, and a 32-bit value from thirty-two to eight. Shorter strings are quicker to read, quicker to type and much less likely to be miscopied, and a mistake in a long run of 1s and 0s is very hard to spot.
That is why hexadecimal turns up wherever binary values are displayed to a person: MAC addresses, IPv6 addresses, memory addresses in error messages and debuggers, machine code listings, and colour codes in web pages and image editors such as #2E8B57. The computer still works in binary throughout; hexadecimal is a presentation layer for human eyes.
Adding two positive 8-bit binary integers
Binary addition uses four rules you already know from denary, applied to a column that can only hold 0 or 1: 0 + 0 = 0; 0 + 1 = 1; 1 + 0 = 1; and 1 + 1 = 0 carry 1 into the next column to the left. When a carry arrives in a column that already has two 1s, you are adding 1 + 1 + 1, which gives 1 with a carry of 1.
Work from the least significant bit on the right towards the most significant bit on the left, and write the carries small above the next column so the examiner can follow them. For 0110 1101 (109) plus 0011 0111 (55): the right-hand column gives 1 + 1 = 0 carry 1, and carrying on to the left produces 1010 0100. Check it in denary: 109 + 55 = 164, and 1010 0100 is 128 + 32 + 4 = 164.
Both numbers must be positive and both must be eight bits for this objective. If a question gives you a shorter value, pad it on the left with zeros so the columns line up, and if it gives you a denary value, convert first and show that conversion as a line of working.
| Column sum | Result bit | Carry out |
|---|---|---|
| 0 + 0 | 0 | 0 |
| 0 + 1 | 1 | 0 |
| 1 + 0 | 1 | 0 |
| 1 + 1 | 0 | 1 |
| 1 + 1 + 1 | 1 | 1 |
Overflow: when the register has no room left
Every register has a predefined limit set by its width. An 8-bit register holds 0 to 255; a 16-bit register holds 0 to 65 535. An overflow error occurs when the result of a calculation is a value outside that limit, so the answer cannot be represented in the space available.
The mechanism is the carry out of the most significant bit. Add 1010 0111 (167) to 1001 1011 (155) and the true total is 322, which needs nine bits. The eighth column produces a carry, but there is no ninth column inside an 8-bit register, so the carry is lost. What remains is 0100 0010, which is 66 — not 322 and not any useful part of it.
A carry out of the most significant bit is therefore the signal that an overflow has happened. In an exam answer, saying 'the answer is bigger than 255' earns the idea; saying 'the carry out of the most significant bit is lost, so the value stored is wrong' earns the explanation. The same reasoning scales: a 16-bit calculation overflows when the result passes 65 535.
Logical shifts left and right
A logical shift moves every bit in the register a fixed number of places. In a logical left shift each bit moves one place towards the most significant end, a 0 is shifted in at the least significant end, and whatever is pushed off the most significant end is lost. In a logical right shift each bit moves one place towards the least significant end, a 0 is shifted in at the most significant end, and whatever is pushed off the least significant end is lost.
The effect on the value is a multiplication or a division by a power of two. One left shift doubles the value, two left shifts multiply it by 4, three by 8. One right shift halves it, two right shifts divide it by 4, and any remainder is discarded. Multiple shifts are just the single shift repeated, so a shift of three places is the same as shifting one place three times.
For 0001 1010 (26): a left shift of 1 gives 0011 0100 (52); a left shift of 2 gives 0110 1000 (104); a right shift of 1 gives 0000 1101 (13); a right shift of 2 gives 0000 0110 (6), because 26 ÷ 4 is 6.5 and the 0.5 disappears with the bits shifted off the end.
The multiplication only holds while nothing important is lost. Left shifting 1011 0010 (178) by one place gives 0110 0100, which is 100, not 356 — the 1 worth 128 has been pushed off the end. So the safe wording is that a left shift multiplies by 2 provided no 1 is lost from the most significant bit, and a right shift divides by 2 with the remainder lost.
| Operation | 8-bit result | Denary | Effect on the value |
|---|---|---|---|
| Start | 0001 1010 | 26 | the original value |
| Left shift 1 | 0011 0100 | 52 | × 2 |
| Left shift 2 | 0110 1000 | 104 | × 4 |
| Right shift 1 | 0000 1101 | 13 | ÷ 2, remainder lost |
| Right shift 2 | 0000 0110 | 6 | ÷ 4, remainder lost |
Two's complement: storing negative integers
There is no minus sign inside a register, so negative integers are stored using two's complement. The most significant bit becomes the sign bit: 0 means the value is positive and 1 means it is negative. Positive values are written exactly as before, with a 0 in the sign position, so +37 is 0010 0101.
To write a negative value, take the positive value in 8-bit binary, invert every bit, then add 1. For -37: +37 is 0010 0101; inverting gives 1101 1010; adding 1 gives 1101 1011. Both steps are needed — inverting alone gives the one's complement, which is one less than the value you want.
To convert back, look at the sign bit first. If it is 0, add the place values as usual. If it is 1, the value is negative: invert every bit, add 1, and put a minus sign in front. For 1110 1000 the sign bit is 1; inverting gives 0001 0111, which is 23; adding 1 gives 24; so the value is -24.
Eight-bit two's complement covers -128 to +127, because half of the 256 patterns are needed for negative values and zero takes one of the rest. 1000 0000 is -128 and 0111 1111 is +127. A nice property is that addition works on negative values without any special rules, which is why computers use this system rather than a separate sign bit.
| Denary | Working | 8-bit two's complement |
|---|---|---|
| +37 | positive, so the sign bit is 0 | 0010 0101 |
| -37 | invert 0010 0101 to 1101 1010, then add 1 | 1101 1011 |
| +90 | positive, so the sign bit is 0 | 0101 1010 |
| -90 | invert 0101 1010 to 1010 0101, then add 1 | 1010 0110 |
| -128 | the smallest value that fits in 8 bits | 1000 0000 |
Key terms you must be able to define
- Binary
- A base 2 number system that uses only the digits 0 and 1. All data is converted to binary before a computer can process it.
- Denary
- A base 10 number system that uses the digits 0 to 9. It is the number system people use for everyday counting.
- Hexadecimal
- A base 16 number system that uses 0 to 9 and A to F. One hex digit represents four bits, so hexadecimal is a shorter way of writing binary.
- Bit
- A single binary digit, either 0 or 1. It is the smallest unit of data and matches the on or off state of one transistor.
- Nibble
- Four bits, which is exactly the amount of binary represented by one hexadecimal digit.
- Place value
- The amount a column is worth in a number system: powers of 10 in denary, powers of 2 in binary and powers of 16 in hexadecimal.
- Register
- A fixed-width row of bits inside the processor used to hold a value while it is being worked on. An 8-bit register can hold 0 to 255.
- Overflow error
- An error that occurs when a calculation produces a value larger than the register can represent, so the carry out of the most significant bit is lost and the stored result is wrong.
- Logical shift
- Moving every bit in a register a set number of places left or right, shifting zeros in at the opposite end and losing any bits pushed off the end.
- Two's complement
- The system used to store positive and negative 8-bit integers, in which the most significant bit is the sign bit and a negative value is made by inverting the bits and adding 1.
- Sign bit
- The most significant bit of a two's complement value: 0 for a positive number and 1 for a negative number.
In context
2210 sets its questions inside systems a student already uses - a school network, a phone, a supermarket till, a vending machine, a website. These show how the theory is applied to a context you have not seen before, then you answer for the system you are given.
A colour on a web page is written as six hex digits, such as #2E8B57. Each pair of digits is one byte giving the amount of red, green and blue, so the whole colour is 24 bits written as six characters. A designer can read and adjust that code; the same colour in binary would be twenty-four 1s and 0s that nobody could check by eye.
A MAC address such as 00:1A:2B:3C:4D:5E is six bytes, and each byte is written as two hex digits. The hardware works with the 48 bits directly; the colon-separated hex form exists so a technician can read the address off a label or type it into a configuration screen without losing a bit.
A game that stores a score in an 8-bit value can count up to 255. If a player passes that limit on a level that awards large bonuses, the carry is lost and the score snaps back to a small number, or in two's complement appears as a large negative value. Players describe this as the counter 'breaking'; the cause is an overflow error.
A tracker stores a sensor reading in 8 bits. To halve the sensitivity of the reading, the firmware performs a logical right shift of one place, which divides the value by 2 and discards the remainder. Shifting twice divides by 4. The same instruction takes a fraction of the time of a division, which matters on a device running from a small battery.
When a program fails, the message often includes an address such as 0x7FFE3A10. That is hexadecimal for a 32-bit memory address: eight hex digits, one per nibble. Programmers use these addresses to find the faulty instruction, which would be far harder if the address were printed as thirty-two bits of binary.
Flashcards
Click a card to flip it. Mark honestly — the tracker uses this.
Exam-style questions
8 questions, 21 marks. Write your answer and press Mark my answer — it is marked against the scheme and tells you which points you made and what would gain the next mark. You can still reveal the scheme and mark it yourself instead.
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Exam technique for this topic
- Write the place values along the top of every conversion before you touch the digits. A conversion error is nearly always a column worth the wrong amount rather than a slip in the adding.
- Group binary into fours from the right when converting to hexadecimal, and pad the left-hand group with zeros if the length is not a multiple of four. Padding on the right gives a different and wrong number.
- In binary addition, write the carries above the next column and work right to left. Then state explicitly whether an overflow occurred and link it to the carry out of the most significant bit being lost.
- When a shift question says 'state the effect', give the arithmetic effect and the condition: multiplied by 4, or divided by 4 with the remainder discarded, and lost bits if a 1 is pushed off the end.
- For two's complement, show the positive value, the inversion and the add 1 as separate lines. A final answer with no working scores nothing if it is wrong, and the add 1 is a mark in its own right.
- Give the answer in the bit width the question asks for. A 16-bit question expects sixteen bits including leading zeros, and a four-digit hexadecimal answer, not a shortened one.
Why students lose marks here
Where this fits in O Level Computer Science 2210
This page is one topic of Cambridge O Level Computer Science 2210. O Level Computer Science 2210 is the course home: every area and every topic in Cambridge's own order, the papers you sit and what each one tests, and the exam-practice tools. The full topic list, with each topic linked to its revision page, is on the O Level Computer Science 2210 syllabus page.