Ready for your next dose of background Computer Science knowledge. In today’s post we build on what we have learnt in my previous post and look at binary addition.

There are a number of ways of performing binary addition and in this post, we’re going to look at two of them.

In the first approach, we use our new knowledge of converting between binary and decimal to convert the numbers back into decimal before performing the addition and then convert the result back into decimal.

The second approach, adds the numbers directly in binary. This approach is a little more complex if you are not used to it but it is based on exactly the same rules that performing addition in decimal is based on. Take things slowly and see how you get on and if you have questions please post them in the comments. First up then is Approach 1 – Converting things back into decimal.

## Approach 1: Binary Addition by Converting Everything to Decimal

As ever, things are best explained with examples so let’s take two binary numbers A = 10010_{2} and B = 11100_{2} and add them together.

In this approach we are first going to convert both numbers into decimal. If you can’t remember how to do this, go back to my previous post on binary and decimal numbers and take another look at the section on **Converting from Binary to Decimal**. Given that knowledge, you should be able to follow the steps below.

First we’ll convert A:

A = (1 * 2^{4}) + (0 * 2^{3}) + (0 * 2^{2}) + (1 * 2^{1}) + (0 * 2^{0})

A = (1 * 16) + (0 * 8) + (0 * 4) + (1 * 2) + (0 * 1) = 18_{10}

Next we’ll convert B:

B = (1 * 2^{4}) + (1 * 2^{3}) + (1 * 2^{2}) + (0 * 2^{1}) + (0 * 2^{0})

B = (1 * 16) + (1 * 8) + (1 * 4) + (0 * 2) + (0 * 1) = 28_{10}

Now that we know the values of A and B in decimal we can simply add them together:

Result = A + B = 18_{10} + 28_{10} = 46_{10}

All that then remains is to convert our result back into binary. Can you remember the steps we take to do that. Here’s a recap of the Division Method from my previous post.

Take our decimal number and divide it by 2 using integer division. Make a note of the new result plus any remainder i.e.

46 / 2 = 23 r 0

Repeat this again, dividing our new number (23) by 2 and again making a note of the result and the remainder.

23 / 2 = 11 r 1.

Keep repeating this process until we have a result of zero and either a 1 or a 0 remainder. The full table is below:

Calculation | Result | Remainder |

46 / 2 | 23 | 0 |

23 / 2 | 11 | 1 |

11 / 2 | 5 | 1 |

5 / 2 | 2 | 1 |

2 / 2 | 1 | 0 |

1 / 2 | 0 | 1 |

So the result of A + B = 10010_{2} + 11100_{2} = 18_{10} + 28_{10} = 46_{10} = 101110_{2} easy isn’t it!

## Approach 2: Binary Addition in Binary Directly

Now, the approach above is a good approach for you and I when we want to add binary numbers (especially if you are still somewhat new to working with binary), but your computer doesn’t work in decimal. It performs the same addition using slightly different approach: adding in binary directly.

Let’s take our two numbers again:

A = 10010_{2}

B = 11100_{2}

Much as you would when you added decimal numbers we can treat each of the columns that make up our binary number and individually add them. As with decimal, we start on the right-hand side with least significant column, the 1’s column, and work right to left. Adding down the columns we get:

32’s | 16’s | 8’s | 4’s | 2’s | 1’s | |

Carry | ||||||

A | 1 | 0 | 0 | 1 | 0 | |

B | 1 | 1 | 1 | 0 | 0 | |

Result |
? |
1 |
1 |
1 |
0 |

When we get to the 16’s column we have a problem. Remember in binary we can only use the digits 0 or 1 but when we add 1 and 1 together we get a result of 2. So what do we do.

Well, the first thing we do is write that out the result as binary (which is 10_{2}) and put the result in the carry row with the least significant bit (the right most bit in our result) in the column we are adding. When we do this, the 1 of our result is now located in the 32’s column. We then strike through the two numbers in the 16’s column we were just adding and resume adding up with the 16’s column. The effect is that we carry out the 1 of the result from the 16’s column into the 32’s column.

32’s | 16’s | 8’s | 4’s | 2’s | 1’s | |

Carry | 1 | |||||

A | 0 | 0 | 1 | 0 | ||

B | 1 | 1 | 0 | 0 | ||

Result |
1 |
0 |
1 |
1 |
1 |
0 |

Once we’ve completed our carry out we then resume adding down the columns to get the same result as our example above: 101110_{2}

### Truth Tables

Let’s extend the example above and look at all the possible situations you might find when adding numbers down one of of our power-of-2 columns and all the possible carries that may ensue.

I’ve tried to illustrate this in the table below (it’s just oriented on its side this time) and have explicitly separated the Carry row from the table above into two more detailed (columns) in our new table; Carry In and Carry Out.

Carry In will be used to represent values that might be carried into a column as a result of adding up a previous column such as in the example above where we carried a value into the 32’s column as a result of adding up the 16’s column.

The Carry Out column will be used to represent the value carried to the next higher power-of-2 column as a result of the addition. An example of this is the 1 we carried out of the 16’s column (and then on into the 32’s column in the example above.

The other column in the table below that you might not have seen before is the Sum column.

The Sum column will represent the value that would be left in the column after we had completed our carrying in, adding and carrying out.

Also notice that if you read the Carry Out and Sum columns as a pair you can see that this is the binary result of adding up the Carry In, A and B columns.

Scenario | Carry In | A | B | Carry Out | Sum |

0 | 0 | 0 | 0 | 0 | 0 |

1 | 1 | 0 | 0 | 0 | 1 |

2 | 0 | 1 | 0 | 0 | 1 |

3 | 1 | 1 | 0 | 1 | 0 |

4 | 0 | 0 | 1 | 0 | 1 |

5 | 1 | 0 | 1 | 1 | 0 |

6 | 0 | 1 | 1 | 1 | 0 |

7 | 1 | 1 | 1 | 1 | 1 |

It’s a short post today, but I’ll leave you with a little teaser question.

**Using the truth table above and what you have learnt today, see if you can work out what the result of adding A and B would be if A = 110011 and B = 101010**

Get in touch if you need some help!

Image credit: http://flic.kr/p/4EeDGe