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 = 010110112 = 9110 and subtract the positive number B = 001001012 = 3710 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 (001001012) we get a Two’s Complement representation of B (B’) of: 110110112.
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 = 5410. If we check our maths in decimal: 9110 – 3710 = 5410. 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 = 011001102 = 10210 and subtract B = 100010102 = 13810 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: 011101102.
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 = 110111002 = -3610. Again if we check this in decimal we have 10210 – 13810 = -3610
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 = 111010012 and B = 111101102? 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