Binary Addition

Once we can convert a decimal number into binary, we can still do any 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:

  1. How to add two binary numbers together.
  2. What overflow errors are and how to they occur.
Media Attachments: Presentation

1. How to Add Two Binary Numbers

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.

1000100101
Number 128
Number 246
Result

Let’s work through this simple addition step-by-step:

1000100101
Number 128
Number 246
Result04
Carry1
We take the first column – add the 8 and the 6, giving us 14 – which means we store 4 in the ‘1’s column and carry over 1.
1000100101
Number 128
Number 246
Result74
Carry1
Then add up the tens column, including the carry, which gives us the decimal value 74.

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:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 10 (0 carry 1)
  • 1 + 1 + 1 = 11 (1 carry 1)

Let’s try using this to add two numbers in binary.

Worked Example 1

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.

1286432168421
Number 100000101
Number 200000101
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.

1286432168421
Number 100000101
Number 200000101
Result0
Carry1
1+1 gives us 0 and carry over 1.

Step 2 – Now let’s look at the 2’s column.

1286432168421
Number 100000101
Number 200000101
Result10
Carry1
0 + 0 + 1 = 1, so there is nothing to carry.

Step 3 – Let’s now look at the 4’s column.

1286432168421
Number 100000101
Number 200000101
Result010
Carry11
1 + 1 = 0, and carry the 1.

Step 4 – Finally, we look at the 8’s column.

1286432168421
Number 100000101
Number 200000101
Result1010
Carry11
0 + 0 + 1 = 1, with nothing to carry.

This gives us a final number of 1010.

Which if we convert to decimal, is 10. The correct answer!

Worked Example 2

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 column.

1286432168421
Number 100000111
Number 200000111
Result0
Carry1
1 + 1 = 0, carry the 1.

Step 2 – Now let’s look at the 2’s column.

1286432168421
Number 100000111
Number 200000111
Result10
Carry11
1 + 1 + 1 = 1, carry the 1.

Step 3 – Let’s now look at the 4’s column.

1286432168421
Number 100000111
Number 200000111
Result110
Carry111
1 + 1 + 1 = 1, and carry the 1.

Step 4 – Finally, we look at the 8’s column.

1286432168421
Number 100000111
Number 200000111
Result1110
Carry111
0 + 0 + 1 = 1, with nothing to carry.

This gives us a final number of 1110.

Which if we convert to decimal, is 14. The correct answer!

Worked Example 3

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:

8421
1011
The decimal value of this number is 11 (8+2+1).
8421
1001
The decimal values of this number is 9 (8+1).

In the decimal world, 11 + 9 = 20.

So now all we have to do is convert the decimal number 20 back into binary.

168421
10100

So there you have it, 1011 + 1001 = 10100.

Further Thought

Which of the two methods of binary addition do you think is better and why?

2. Overflow Errors

Let’s assume we want to add two 4 bit binary numbers – 9 and 9 in decimal.

8421
Number 11001
Number 21001
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.

8421
Number 11001
Number 21001
Result0
Carry1

Step 2 – Now let’s look at the 2’s column.

8421
Number 11001
Number 21001
Result10
Carry1

Step 3 – Let’s now look at the 4’s column.

8421
Number 11001
Number 21001
Result010
Carry1

Step 4 – Finally, we look at the 8’s column…

8421
Number 11001
Number 21001
Result0010
Carry1

… 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:

168421
Number 11001
Number 21001
Result10010

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.

Further Thought

Hackers can exploit overflows to attack vital areas of memory. Research about this online.

Lesson Summary

So to summarise what we’ve learnt in this lesson:

  • Once we can convert a decimal number into binary, we can still do any mathematics on them as if they were decimal numbers.
  • In order to be able to add two binary numbers together, you need to learn the rules of binary addition. These are:
    • 0 + 0 = 0
    • 0 + 1 = 1
    • 1 + 0 = 1
    • 1 + 1 = 10 (0 carry 1)
    • 1 + 1 + 1 = 11 (1 carry 1)
  • If the number of bits required to store the answer is bigger than the number of bits allocated then this is an overflow error.