How to Subtract In Binary Using 2'S Complement

Friday, March 22, 2019

How to Subtract In Binary Using 2'S Complement


Interested in subtracting in binary using 2's complement? In this final episode in the Bits of Binary series, Harley begins with what we know about subtracting in decimal and applies that to binary. Along the way way he introduces 2's complement and shows how it makes subtracting binary numbers a matter of simple addition.

Here at House of Hacks we do tutorials, project overviews, tool reviews and more related to making things around the home and shop. Generally this involves wood and metal working, electronics, photography and other similar things. If this sounds interesting to you, you may subscribe here.

If you’re interested in learning more about the House of Hacks' values, here’s a playlist for you.

And here’s the most recent video.

For a written transcript, go to How to Subtract In Binary Using 2'S Complement

Music under Creative Commons License By Attribution 3.0 by Kevin MacLeod at
Intro/Exit: "Hot Swing"


This is an elegant but non-intuitive way to represent negative numbers.

On this episode of Bits of Binary, I'll explain how it works, here at the House of Hacks.

Hi Makers, Builders and Do-It-Yourselfers. Harley here.

This is the last planned episode in the Bits of Binary series.

Previous episodes explored the concept of alternate number systems and how to do various mathematical operations in binary.

If you missed any of these episodes or want a refresher, they're all in this playlist.

In this past episode, I explained how to subtract a smaller number from a larger one.

It introduced subtraction and related it with how you know how to do it in decimal.

But by limiting the scope to smaller numbers subtracted from larger ones, I avoided the topic of how to deal with negative numbers.

Let's look at negative numbers and how computers represent them.

When learning decimal in grade school, we started with only positive numbers.

Once we got that under our belt, we were introduced to negative numbers, where the number line just mirrored the positive side with a negative sign in front of the number.

We could do the same thing with binary; just add a negative sign in front.

And if humans were the primary users of binary math, this would probably be sufficient, just like it is in decimal.

But we have to remember that the most common use of binary numbers is inside computers.

In the digital electronic environment of voltages, currents and electrons, we don't have the luxury of introducing another abstract symbol like a minus sign.

Ultimately, everything has to reduce to an on or off value.

Further, because of the physicality of the equipment, representing numbers of arbitrary length is hard.

So, two things are done to simplify the problem for computers to use.

First, there are already two symbols, 0 and 1 represented by no voltage and a voltage.

Rather than introducing a third symbol like a minus sign, a 1 is used to represent a minus and a 0 is used to represent a plus.

Second, instead of allowing an arbitrary number of digits, a fixed number of digits is defined.

Typically the number of digits are a power of two: 4, 8, 16 and so forth.

For the purposes of example, we'll assume 8 digits.

If we assume an unsigned number, 8 digits gives us a range of 0 to 255, or all zeros to all ones.

If we use a 1 in the most significant bit to represent a negative number and a 0 to represent a positive number, we reduce the number of bits for the number by one to 7.

This gives us a range of 0 to 127. With the last bit for a sign, the total range is 0 to +/- 127.

This introduces some interesting problems.

In this scenario, you have 0 to 127 in the positive range and then you add a 1 to the front for negative numbers.

Look what happens here at 0.

You have both a positive zero and a negative zero.

This doesn't make too much sense from a mathematical perspective.

The other problem is how to simply handle negative numbers?

First let's look at a decimal example to remember how borrowing works.

Let's take 2002 - 3.

We have 2 - 3, we can't do this so we try to borrow 1 from the previous column.

The previous column is already 0 so we have to borrow from its previous column.

Well this is also 0 so we do it again.

This means the two changes to a 1 and the third column is 10.

Now we borrow the 1 making the third column 9 and the second column 10.

Now we can borrow the original 1 we needed leaving the second column 9 and the first column 12.

Now the subtraction can occur with a final value of 1999.

Remember how this borrowing against 0 columns causes them to become 9s.

We'll come back to this.

Let's look at another example of 1 minus 2.

According to our earlier rule, we can't take a larger number from a smaller number.

So we make a new rule that says if we have a larger number from a smaller number, then swap the order and and add a minus sign to the result.

We could in theory do this in electronics.

Do a comparison, swap the order if needed and add the minus sign.

While it can be done, it's pretty complicated circuitry.

Let's look at what happens if we try doing some borrowing.

Using our example of 1 - 2, in binary we have 1 - 10, we can't do this so we have to borrow from a previous column.

But since the previous column is 0 so we go to the next column.

We do this until we run out of columns.

So we set the negative sign and make this last column 10, or the value 2.

Now we borrow one from that column, leaving 1 and shift to the next column.

This borrowing propagates all the way back leaving 1s in each column, just like we had 9s in our earlier decimal example.

Finally back to the first column, with the borrow, the subtraction can be done, leaving 1.

Because we have 0s in all the remaining columns, the borrows drop down, like the 9s did in our first decimal example.

This gives us a final value of 11111111.

If we wanted to make this look like the original idea of using the last bit for a sign and the remaining bits look just like their positive counterparts, somehow we need to make this look like 10000001.

We need to figure out how those 1s that are a result of borrowing get changed back to zeros.

It turns out this is a pretty complicated problem too.

But what happens if we just assign the result of all ones to mean -1?

Ultimately numbers are just arbitrary symbols with assigned values.

There's nothing stopping us from just saying this symbol represented by all 1s has the value -1.

If we do this, it really simplifies the electronic circuitry for the computer designers.

Let's take look at a couple other examples.

Let's take 1 - 3.

In binary this is 1 - 11.

Going through the process we used for 1 - 2, we end up with a final result of 11111110 which we assign the value of -2.

Now let's take 1 - 4. In binary this is 1 - 100.

Going through the process again, we end with a final result of 11111101 which we assign the meaning of -3.

Doing this again for 1 - 5 gives us 11111100 meaning -4.

Is there a pattern emerging?

If we change all the 1s to 0s and 0s to 1s we see this.

And this is the positive values offset by 1.

So there is a pattern and it's pretty logical.

And it turns out doing this offset by one and change of 0s to 1s and 1s to 0s is relatively easy to do in electronic circuitry.

It's called two's complement and has become the standard way of representing negative numbers.

Going back to our school days learning decimal numbers, once we learned about negative numbers, we then learned that all subtraction problems can be transformed into problems where we add the opposite.

So 2 - 1 becomes 2 + -1 and 1 - 2 becomes 1 + -2.

Well the same thing happens in binary using two's complement notation for negative numbers.

2 - 1 becomes 2 + -1.

Working through this, 0 + 1 is 1.

1 + 1 is 0 carrying a 1.

This 1 + 1 continues until we run out of digits leaving us with 00000001, the expected answer.

If we switch the operands, we have 1 - 2.

Converting this to adding the opposite gives us this.

Working through this 1 + 0 is 1.

The remaining columns are all 0 + 1 giving us 1s for a final result of 11111111 or -1.

Again the expected answer.

And it turns out this works for any combination of numbers.

Let's look at this on a number line.

And remember that weird case we had before with a positive zero and negative zero?

Well, it turns out with two's complement math, what used to be that hard to deal with negative zero just becomes -128.

Here at House of Hacks, I hope to inspire, educate and encourage people with a bent towards making technical and mechanical things out of wood, metal, cameras and electronics.

Binary is the basis for many things related to digital electronics, as well as Boolean algebra, which I'll cover in a future series.

If topics such as these are interesting to you, be sure to subscribe and click the bell notification icon to have YouTube alert you when future videos are uploaded.

Thanks for joining me on our creative journey.

Until next time, go make something. Perfection's not required. Fun is!