Now, we have already talked about how to perform binary addition but what about the opposite? How do we subtract two binary numbers? In today’s post, we’re going to walk through a couple of step-by-step examples.

Before we dive in, first let’s take a step back and look at the problem of subtraction as it relates to computers.

If we wanted to perform the following calculation:

C = A – B

You and I would perform that calculation by simply subtracting, just as we were taught in school. Computers though, don’t perform subtraction; they don’t have the circuits built in that support it. Instead they take another approach. Take a look at this. We could rewrite the equation above as the following. Mathematically, it is exactly the same operation:

C = A + (-B)

And that’s the secret:

Computers actually perform subtraction by adding.

Now, given everything we have talked about up until this point, we already have all the skills and knowledge that we need to perform this sort of operation ourselves:

- We already know how to add two binary numbers together – see this post.
- We also know how to negate a number, turning it from it’s positive to its negative counterpart (or visa versa) – see this post.

So given our skills and armed with the knowledge above. Let’s see if we can do it ourselves.

### Binary Subtraction Using Two Positive Binary Numbers

In our first example, we’re going to start simple.

Imagine that we are going to take the following positive number A = 01011011_{2} = 91_{10} and subtract the positive number B = 00100101_{2} = 37_{10} to get our answer C.

The steps are as follows.

**Step 1:** Calculate the Two’s Complement of our number B.

Remember the steps to create a Two’s Complement number; Flip all the bits and then add 1.

If we do this to the bits in B (00100101_{2}) we get a Two’s Complement representation of B (B’) of: 11011011_{2}.

**Step 2:** Add the Two’s Complement of B (B’) to A to Get Our Result (C)

I’ve included the calculation below to add the two numbers in binary.

**Remember**: Start at the least significant bits and then perform the necessary carries. Take your time. If you get stuck, go back and read my post on adding binary numbers.

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

Carry | 1 | 1 | 1 | 1 | 1 | |||

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

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

C | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |

In this case, we would also have a bit that was carried out as a result of adding the bits in the 128’s column.

**Remember**: We are working with Two’s Complement numbers and what did we learn about Two’s Complement when we need an additional column? That’s correct, we simply drop the bit.

The result (C) is therefore 001101102 = 54_{10}. If we check our maths in decimal: 91_{10} – 37_{10} = 54_{10}. We’re all good.

### Subtracting a Negative Binary Number from a Positive Binary Number

Let’s take a look at another example. What would we do if we wanted to take A = 01100110_{2} = 102_{10} and subtract B = 10001010_{2} = 138_{10} to get the answer C?

**Step 1:** First we calculate B’ by creating the Two’s Complement version of B by flipping the bits and adding 1.

B’ is therefore: 01110110_{2}.

**Step 2:** The next step is to add A and B’ together:

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

Carry | 1 | 1 | 1 | 1 | ||||

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

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

C | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 |

So C = 11011100_{2} = -36_{10}. Again if we check this in decimal we have 102_{10} – 138_{10} = -36_{10}

### Subtracting a Negative Binary Number from a Negative Binary Number

**The next one is for you to see if you can work it through on your own. **

**If C = A – B, what would the value of C be when A = 11101001 _{2} and B = 11110110_{2}? Post your answers in the comments below!**

Hint: It is exactly the same two-step process as we’ve followed in our examples above.

Image credit: http://flic.kr/p/cqYygQ