Even when a decimal number is converted to binary, we can still perform mathematics on them as if they were decimal numbers. One of the mathematical operations is simple addition, where we can add two numbers and get a result.
However sometimes the result of the answer can be too big for our number of bits, and this is called an overflow.
In this lesson, we’ll learn about:
This is the one piece of maths that you need to be able to do for the exam. You will not have access to a calculator.
Let’s take some simple addition in normal decimal notation. Say we wanted to add the numbers 28 and 46.
1000 | 100 | 10 | 1 | |
---|---|---|---|---|
Number 1 | 2 | 8 | ||
Number 2 | 4 | 6 | ||
Result |
Let’s work through this simple addition step-by-step:
1000 | 100 | 10 | 1 | |
---|---|---|---|---|
Number 1 | 2 | 8 | ||
Number 2 | 4 | 6 | ||
Result | 0 | 4 | ||
Carry | 1 |
1000 | 100 | 10 | 1 | |
---|---|---|---|---|
Number 1 | 2 | 8 | ||
Number 2 | 4 | 6 | ||
Result | 7 | 4 | ||
Carry | 1 |
So we have a decimal value of 74. This is something I am sure you’re very familiar with.
In order to be able to add two binary numbers together, you need to learn the rules of binary addition. These are:
Let’s try using this to add two numbers in binary.
Let’s try adding 101 and 101.
Now 101 is the number 5, so 5 + 5 should equal 10.
10 in binary is 1010. Let’s see if knowing the answer means we can work it out correctly.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Result | ||||||||
Carry |
Just like with the decimal addition, we’ll work through this step-by-step:
Step 1 – Let’s start by looking at the 1’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Result | 0 | |||||||
Carry | 1 |
Step 2 – Now let’s look at the 2’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Result | 1 | 0 | ||||||
Carry | 1 |
Step 3 – Let’s now look at the 4’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Result | 0 | 1 | 0 | |||||
Carry | 1 | 1 |
Step 4 – Finally, we look at the 8’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
Result | 1 | 0 | 1 | 0 | ||||
Carry | 1 | 1 |
This gives us a final number of 1010.
Which if we convert to decimal, is 10. The correct answer!
Let’s say we wanted to add 111 and 111.
111 is 7, so 7 + 7 should equal 14, which is 1110 in binary.
Step 1 – Let’s start with the 1’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Result | 0 | |||||||
Carry | 1 |
Step 2 – Now let’s look at the 2’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Result | 1 | 0 | ||||||
Carry | 1 | 1 |
Step 3 – Let’s now look at the 4’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Result | 1 | 1 | 0 | |||||
Carry | 1 | 1 | 1 |
Step 4 – Finally, we look at the 8’s column.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|---|---|---|
Number 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Number 2 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
Result | 1 | 1 | 1 | 0 | ||||
Carry | 1 | 1 | 1 |
This gives us a final number of 1110.
Which if we convert to decimal, is 14. The correct answer!
There is another way of checking your work. That is to convert your binary numbers to decimal, add them and then convert your answer back again.
Say you wanted to add two numbers, 1011 and 1001.
The first thing you do is find the decimal values of these two binary numbers:
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 1 | 1 |
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 0 | 1 |
In the decimal world, 11 + 9 = 20.
Now all we have to do is convert the decimal number 20 back into binary.
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 0 | 1 | 0 | 0 |
So there you have it! 1011 + 1001 = 10100.
Which of the two methods of binary addition do you think is better and why?
Let’s assume we want to add two 4-bit binary numbers together – 10001 and 1001 (or 9 and 9 in decimal).
8 | 4 | 2 | 1 | |
---|---|---|---|---|
Number 1 | 1 | 0 | 0 | 1 |
Number 2 | 1 | 0 | 0 | 1 |
Result | ||||
Carry |
Following the rules we learnt in the previous section, answering this question should go as follows:
Step 1 – Let’s start by looking at the 1’s column.
8 | 4 | 2 | 1 | |
---|---|---|---|---|
Number 1 | 1 | 0 | 0 | 1 |
Number 2 | 1 | 0 | 0 | 1 |
Result | 0 | |||
Carry | 1 |
Step 2 – Now let’s look at the 2’s column.
8 | 4 | 2 | 1 | |
---|---|---|---|---|
Number 1 | 1 | 0 | 0 | 1 |
Number 2 | 1 | 0 | 0 | 1 |
Result | 1 | 0 | ||
Carry | 1 |
Step 3 – Let’s now look at the 4’s column.
8 | 4 | 2 | 1 | |
---|---|---|---|---|
Number 1 | 1 | 0 | 0 | 1 |
Number 2 | 1 | 0 | 0 | 1 |
Result | 0 | 1 | 0 | |
Carry | 1 |
Step 4 – Finally, we look at the 8’s column…
8 | 4 | 2 | 1 | |
---|---|---|---|---|
Number 1 | 1 | 0 | 0 | 1 |
Number 2 | 1 | 0 | 0 | 1 |
Result | 0 | 0 | 1 | 0 |
Carry | 1 |
… and there’s 1 to carry over, but now we have a situation called an OVERFLOW.
The resulting binary number needs more bits to store it than the original two numbers. The actual result of adding 9 + 9 would give us 18, which in binary would look like this:
16 | 8 | 4 | 2 | 1 | |
---|---|---|---|---|---|
Number 1 | 1 | 0 | 0 | 1 | |
Number 2 | 1 | 0 | 0 | 1 | |
Result | 1 | 0 | 0 | 1 | 0 |
Do you see how the product of the addition needs an extra bit to store the information? This is called an OVERFLOW ERROR. This means that the number of bits required to store your answer is bigger than the number of bits allocated.
Hackers can exploit overflows to attack vital areas of memory. Research about this online.
So to summarise what we’ve learnt in this lesson: