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

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.

Wednesday, November 26, 2014

Bits of Binary: How to add binary numbers



Description

Addition is probably one of the most common operations when using binary numbers. And it's really easy to do. We'll see how easy in this episode of Bits of Binary at the House of Hacks.

How to convert between decimal and binary.
Bits of binary playlist

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

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

Transcript

One plus one equals... huh? I'll talk about how this actually makes sense, today at the House of Hacks.

[Introduction]

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

In the last episode of Bits of Binary, I showed how to convert between decimal and binary numbers. In this episode in the series, we'll look at how to add binary numbers together.

Remember in grade school when you had to memorize this addition chart?

Well, OK, maybe you didn't have to memorize it, but I sure did.

This table is a matrix with the 10 numbers found in the decimal system, 0 through 9, on both the row and column headers. Each cell contains the sum of its row and column header. This gives us the sums for all the single digit combinations. 0+0=0 all the way up to 9+9=18. Multi-digit numbers can be added by simply thinking of them as multiple single digit combinations.

Well, binary has something similar, but much, much smaller. Since there are only two numbers in the binary system, 0 and 1, the table only has two rows and two columns. And it looks like this.

Or if you want to write it a slightly different way as equations, it looks like this.

Once you know this table, the process of adding in binary is exactly the same as adding in decimal. For example let's look at the decimal numbers: 321 + 181. Staring with the units: 1+1 = 2, 2+8 = 10 so write 0 and carry a 1, 1 + 3 = 4 + 1 = 5.

Similarly, in binary we'll look at 1011 + 10. Starting with the units on the right: 1 + 0 = 1, 1 + 1 = 10 so write 0 and carry a 1, 1 + 0 = 1, 1 + 0 = 1 again.

That's it. Addition is short and sweet. Thanks for watching this episode of Bits of Binary. In the next episode, we'll look at how to subtract binary numbers.

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

Thanks to everyone who has subscribed to this channel and liked the videos.

Be sure to leave a comment if you have any thoughts or questions on this topic.

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

Thursday, July 10, 2014

Bits of Binary: How to convert between binary and decimal


Description
Learn how to convert from binary to decimal numbers and from decimal to binary numbers.

There's a written transcript for How to convert between binary and decimal.

There's also a playlist for all the Bits of Binary episodes.

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

Transcript

If you don't get this classic joke, by the end of this episode you should. Today I explain how to convert binary to decimal, and back again, here at the House of Hacks.

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

In the last two episode of Bits of Binary, I introduced alternate number systems in general and the binary system in particular. Next I showed how you can use binary to count much higher than ten on just your fingers. I closed with the question "How high can you count in binary on both hands?" If you came up with the answer 1023, you really understand the basics.

In this episode in the series, I'm going to show how to convert from binary to decimal and from decimal to binary.

Last time, I explained how each column in a number is the base number raised to a power times the value of the column. That sounds more complicated than it is. In our familiar base 10, or decimal, system, the columns are 10^0, 10^1, 10^2 and so forth. This gives us columns that represent units, tens, hundreds and so on. To get a specific number, say 123, you simply multiply the number in the column by the column's value. Or (100 * 1) + (10 * 2) + (1 * 3). Applying this principal to binary, the columns are 2^0, 2^1, 2^2 and so forth. Giving us 1, 2, 4, 8 and on up.

So, let's convert from binary to decimal. What's the decimal value of the number 10101? Given that each column represents a power of two and each column can only have a value of 0 or 1, this means its value is (2^4 * 1) + (2^3 * 0) + (2^2 * 1) + (2^1 * 0) + (2^0 * 1). Removing the items multiplied by zero gives us (2^4 * 1) + (2^2 * 1) + (2^0 * 1). Evaluating the exponents gives us (16 * 1) + (4 * 1) + (1 * 1). And all this simplifies to 16 + 4 + 1 or 21 in decimal.

Now that we know the theory, let's look at some shortcuts. Instead of looking at the columns as 2 to a power, we can look at them with specific values. Starting with the units column, we know it's one. Each subsequent column is the current column times 2. This gives us 1, 1 * 2 is 2, 2 * 2 is 4, 4 * 2 is 8, 8 * 2 is 16 and so on. Next, all we need to do is write the binary number below the numbers: 10101. And then simply add the values of the columns with 1's in them. 16 + 4 + 1 = 21 decimal.

Binary to decimal is really pretty simple.

Next, let's convert from decimal to binary. This is slightly more complicated, but still not hard.

We need to start with a binary column value larger than our decimal number. So, we start at the right side with one and multiply by two until we have a number larger than what we want to convert. Then working from the left we apply this rule: if the value we want to convert is greater than or equal to the column value, then we set a one for that column and subtract the column's value, otherwise, we set a 0 for that column and continue. The result is then applied to the next column. And we apply the rule until we reach zero.

Let's try the earlier example of 21 decimal. First, find the columns. Start with 1 and double until we have a value greater than 21. Then start from the left and apply the rule. 21 is less than 32 so we write a 0 and move to the next column. 21 is greater than 16, so we write a one below the 16 and subtract 16 from 21 leaving us 5. Next column. Five is less than 8 so we write a zero below 8 and move on. Five is greater than 4 so we set a one below the four and subtract four from 5 leaving us 1. One is less than 2 so we set a zero below the 2 and move to the units. One is equal to one so we set a 1 in the units column, subtract 1 from one leaving us zero and we're done.

If we get to the end without reaching zero, we've done something wrong and need to recheck our work.

Looking at the binary value, we have 10101, which is what we saw in the previous example of binary to decimal, so it all works.

Let's try 24 decimal as another example. 24 is greater than 16, so we set a one below the 16, subtract 16 from 24 leaving us 8. 8 is equal to 8 so set a one below the 8 and subtract 8, leaving us 0. Since we know 0 is less than all the other columns, we can just set them to 0 and be done. This leaves us 11000 binary.

That's all there is to convert between binary and decimal.

I've created a playlist over here that contains all the episodes in this series so far and will be filled in as more are added.

Thanks for watching and if you learned something, I'd appreciate a thumbs up If you have any questions or comments, leave them below. I try to respond to all of them.

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

Tuesday, April 8, 2014

Bits of Binary: How to count in binary


Description

For a written transcript, go to How to count in binary

Music under Creative Commons License By Attribution 3.0.
Intro/Exit: "Hot Swing" by Kevin MacLeod at http://incompetech.com
Incidental: "Feelin Good" and "Cold Funk" by Kevin MacLeod at http://incompetech.com

Transcript

If you recognize this as the number 31, you can skip this video. But if you want to know how to count to 1023 on just your fingers, we'll find out in this episode of the House of Hacks.

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

In the last episode of Bits of Binary, I talked a bit about different binary systems in general and binary numbers in particular. You can click here if you're interested in this introduction.

In this episode, I'll start with the basics of decimal numbers that you may already know. I'll then relate that to alternate number systems in general and then binary numbers specifically. Finally I'll wrap up with how to count in binary.

When we were young, we learned to count on our fingers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

A couple years later we learned there's actually a special value that's no quantity: 0. And and with this new knowledge we found that 10 really isn't a number in the same way as the other nine are. It's a combination of 1 and this new non-value value. We found that at the core, we have 0 through 9 as our ten digits, not 1 to 10.

While were were still reeling from this new information, we learned you can count the groups of 0 through 9 and keep track of that in the "ten's column." So, now we could have 10 through 19 and 20 through 29 all the way up to 90 through 99. And then we could add a "hundreds column" for 100 through 199 and so forth. Numbers could actually be arbitrarily large by just adding another column.

This was mind blowing!

There were numbers incomprehensibly large to our young minds like "million" and "billion" with all kinds of crazy numbers of columns.

Then as we grew and got more sophisticated, we learned about something called exponents. These columns we were so comfortable with could now be represented by 10 raised to number of the column. So the "ten's column" was 10^1 and the "hundred's column" was 10^2. The "unit's column" took advantage of a weird property of exponents that said "anything raised to the power of 0 is 1."

It was explained any number could be split into its constituent parts by taking the digit in each column and multiplying it by the power of 10 for that column and adding the results together for the other columns. We found the simple columns we learned early in our education were just shorthand for much more heady concepts.

For example, the number 123 could be written (1 x 10^2) + (2 x 10^1) + (3 x 10^0).

If you're anything like me, this is about where the educational system stopped. There was no direct talk about this "5" being an abstract symbol for an underlying value. They did talk about it in an oblique way when they talked about other cultures having other number systems such as Roman numerals I, V, X, L and C. But that was about it. It was simply given that "5" meant "*****" this many things.

This was the decimal number system and how those of us now middle aged in the United States probably learned it.

If you had your young mind blown when you learned the ten's column was 10 raised to 1 and the hundred's column was 10 raised to 2, here's another mind blowing revelation...

The column base, the 10 so far, doesn't have to be limited to the number 10. It can be any anything!!

For example, this base could be 16 where you'd have the familiar zero and 15 other symbols. In this base, this many objects "**** **** **** ****" would be written as 10. Even though it looks like ten, it isn't. It's sixteen. And if you add one more "*", it'd be written 11 meaning seventeen.

Or the base could be 8 where you'd have zero and 7 other symbols. In this case the quantity seventeen would be represented by the series of digits 21. But in decimal, it'd still be represented by 17 and in base 16 it'd still be 11.

When dealing with multiple number systems at the same time, typically we put a subscript after the number to indicate the base for that number. So in the example of seventeen items, the previous bases would be written as 11(16), 21(8) and 17(10). This helps reduce confusion. But typically only one system is used at a time, and the base is left off, since it's implied by the context.

As you're trying to get your brain around all this, let's talk specifically about today's topic: binary. Its base is 2 so all we have is zero and one other digit: 1. That's it. 0 and 1, 1 and 0. Easy!

Let's look again at the columns we learned about when we learned about exponents. Just like in decimal where column one was 10 to the 0 and column two was 10 to the 1 and column three was 10 to the 2 and so on, in binary column one is 2 to the 0 and column two is 2 to the 1 and column three is 2 to the 2. That means the value of these columns if we multiply them by 1, are 1, 10 and 100 for decimal. And for binary they are 1, 2, 4, 8 and so forth.

Going back to our earlier example, in decimal, for each column's power of ten, you can multiply it by a number from 0 to 9, because your base is 10.

Binary however is much easier. For each column's power of two, you can only multiply it by either 0 or 1, because that's all the digits we have.

So, let's see what happens when we count from 0 to 10. For comparison, let's see both decimal and binary side by side. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

See this pattern...

If we use a finger to indicate one and apply this pattern to our fingers...

zero, one, two, three, eight, nine, ten.

By continuing this pattern, on one hand we can count to the number 31. This is 16. Plus 8. Plus 4. Plus 2. Plus 1. That totals 31. So, how high can you count using both hands?

Thanks for watching this episode of Bits of Binary. In the next episode, we're going to look at how to convert back and forth between binary and the more familiar decimal numbers.

And 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 by hitting the "like" button.

If you are already a subscriber, "thank you!" but if you aren't, be sure to "subscribe" so you don't miss future videos.

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

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