April 2015

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.