House of Hacks: subtraction
Showing posts with label subtraction. Show all posts
Showing posts with label subtraction. Show all posts

Friday, March 22, 2019

How to Subtract In Binary Using 2'S Complement


Description

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 http://incompetech.com.
Intro/Exit: "Hot Swing"

Transcript

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!

Sunday, April 5, 2015

Bits of Binary: How to subtract binary numbers



Description

Subtraction can be done two ways using binary numbers. This episode talks about unsigned subtraction, very similar to how we do it in decimal notation. We'll dive into the details in this episode of Bits of Binary at the House of Hacks.

Links

Bits of binary playlist
What is binary?
How to count in binary
How to convert between binary and decimal
How to add binary numbers

For a written transcript, go to How to subtract binary numbers.

Credits

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

Transcript

That may look confusing on the surface, but if you saw the last Bits of Binary episode, it might make some sense. I'll explain it in more detail in this episode of the House of Hacks.

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

This is a continuation in the series on Bits of Binary. In previous episodes I explored the concept of binary numbers, how to count in binary, how to convert between binary and decimal and, in the last episode, I showed how to add binary numbers together. In this episode, I'll show the simple, obvious way to subtract them that's analogous to how we first learned it in decimal. In a future episode I plan to introduce the non-intuitive way negative numbers are stored in computers and how that impacts subtracting binary numbers.

Remember last time when we talked about addition, we looked at these two tables...

Let's remember how we use this with decimal numbers. We'll ignore negative numbers for now so we'll establish the rule that the first number has to be larger than the second. Since this half of the table is the same as this half, we’ll just ignore one side.

When describing the process, we'll use the example 8 - 5.

The process is to first find the entries in the table for the first number. Then, of those entries, you find the one with the other number in the header. The answer is the other header value.

Binary is exactly the same way, just with the much smaller table. Or written as a series of equations, it looks like this.

The first three probably make intuitive sense as they are the same as decimal. But the last one may not be quite so obvious. Remember that in binary the value for two is represented by 1, 0.

If we recall from grade school, with multi-column numbers, when we subtract a larger number from a smaller one, we have to borrow from the next higher column. Let's take for example 21 - 13 in decimal. The units column is 1 - 3. Well, we can't do that, so we borrow a one from the 2 in the second column giving us 11 - 3. This gives us 8. Moving to the next column we now have 1 - 1, giving us 0.

Binary works exactly the same way. Let's look at some examples in binary.

First something simple: 6 - 2. The units column is 0 - 0 equals 0. The next column is 1 - 1 equals 0 again. The final column is 1 - 0, giving us 1. This gives us an overall result of 100, or the value 4.

Now let's do something with some borrowing: 6 - 3. The units column is 0 - 1. We can't do that so we're going to borrow a 1 from the next column. Now we have 10 - 1 giving a result of 1. Because of the previous borrow, the second column is 0 - 1. So again we borrow from the next higher column giving us 10 - 1 with a result of 1. The final column is 0 (because of the previous borrow) - 0 giving us 0. Overall, the result is 11, or a value of three.

And that's it. Subtraction is a bit more complicated due to the borrowing, but again, it's a known concept just applied in a slightly different way.

Thanks for watching this episode of Bits of Binary. As I mentioned earlier, a future episode will explain how negative numbers are handled in a computer and a less-intuitive but ultimately easier way to handle subtraction. But in the next episode, I'll look at multiplying binary numbers together.

I've created a playlist over here that will be filled in as more episodes in this series are added.

If you liked this, let me know with a "thumbs up.".

If you have any thoughts or questions on this topic, I'd love to hear them in the comments below.

If you're already a subscriber, "thank you!" and if you haven't done so already, be sure to "subscribe" so you don't miss future on topics such as this one, high-speed photography, making a digital computer with 19th century technology and much more.

Until next time, go make something. It doesn't have to be perfect, just have fun.