 Hello and welcome back. I'm Chris Mayfield and you're watching JMU CS 101 week two. This week we're talking about binary numbers and how everything in a computer is stored as ones and zeros. So to help us understand a little bit about how the binary system works, I'd like you to get an index card, pair of scissors, a ruler, if you have one, and a pen. The first thing I like you to do is divide your index card into eight columns using the rulers. So if you're using a five-by-eight index card, go ahead and draw a line every inch down the card until you have eight columns. By the way, in this video and all the videos of this class for that matter, you should feel free to pause and rewind at any moment because I'm going to go through them rather quickly. So after you've gone ahead and divided your index card into eight columns, you should also divide it into four rows. So this is what my index card looks at this point. I just went ahead and drew lines so that there were eight columns and four rows. Now for our next step, I'd like you to label the top row of the card with the powers of two. So on the very left of the card, 128, then 64, 32, 16, 8, 4, 2, and 1, and in the row beneath that, we'll go ahead and write it in exponent notation. So I've got 2 to the 7th, 2 to the 6th, 5, 4, 3, 2, 1, followed by 2 to the 0 on the far right. You're probably wondering what we're going to do with this card. And the basic idea is I want you to create your own binary calculator. So in order for this to calculate a binary, we need to cut out the bottom part of the card. And the way you cut this out is you go ahead and cut from the bottom of the card up to the second to the last row. So let me go ahead and show you one of these. So on the very bottom of the card, I now have this piece of paper here that I can flip like a bit. Now what I'd like you to do at this point is go ahead and cut out the rest of the bottom row. Label this row here where my finger is with all zeros. And on the back side of the bottom row, label each piece of paper with the number one. I'll go ahead and finish that and show you what the final product looks like. So at this point, your card should look like this. I've got all the bottom rows sliced out. And above the slices, I have zeros everywhere. And on the back of the card, I have ones on all the individual bits. So how does this binary calculator work? Let's say I wanted to represent the number 10. Well, 10 in binary is the value 8 plus 2. And so I just have to flip the 8-bit and flip the 2-bit. Therefore, the binary for 10 is 1010. Or I can do it the other way around. What is the binary number represented by 11000? So if I had those two bits flipped, it's basically the same as doing 16 plus 8, which is 24 the last time I checked. So this is a way that you can convert numbers from decimal to binary and vice versa. And it does a nice job of showing how all you're doing is adding powers of 2. And you really should memorize the powers of 2 from 1 to 8. I mean, they're just used so common in computing when you're looking at binary data. It's just nice to be able to rattle right off the top of your head. 2 to the 7th is 128. And 2 to the 8th, for that matter, is 256. And this is where all of those numbers come from that you've seen in computer specs, like 64-bit or 256 megabytes. They're always powers of 2 because they're nice round numbers in terms of binary. Now, there's really nothing special about binary numbers. In fact, they work the same way as the base 10 numbers that you're accustomed to from elementary school arithmetic. Let me write, for example, the number 1, 2, 3, 4, 5 in base 10. And sometimes when we show base 10, we write a subscript with a 10 there. You know, this number right here really means 10,000 plus 2,000 plus 300 plus 40 plus 5. And you saw with the index card, instead of adding powers of 10, we were adding powers of 2. That's what makes it a base 2 number system. Let's take a look now at what arithmetic is like in binary. So we all know from elementary school 0 plus 0 is 0. And 1 plus 0 is 1. And 0 plus 1 is likewise 1. But what's 1 plus 1? Now be careful, your intuition is going to tell you 2. But there is no 2 in binary. You can only have 1 or 0. And if you go back to your index card, 2 is actually the number 1, 0. So I don't know if you've ever seen those t-shirts that say there's only two types of people in the world, those who understand binary and those who don't. But the number 2 is written like a 10. So people who don't understand binary are like, I thought you said there were 10 types of people. Anyway, I'll post a link. Now that's how you add a single bit or a pair of bits in binary. Let's go ahead and add entire numbers. Let's say I want to add the numbers 101 plus 11. So this would be the number, let's see here, 1 plus 0 plus 4 is 5. And this would be the number 1 plus 2, which is 3. So in decimal, it's like we're doing 5 plus 3, which is 8. Let's see how we get that binary value 8. So 1 plus 1, let's see here, is 1, 0. So I have to write the 0 down at the bottom and carry the 1. Just like in traditional arithmetic, if you have more than one digit that will fit at the bottom of the sum, you have to carry the most significant digit. So now we have 1 plus 0 plus 1, which we know is just 1, 0 again. 1 plus 1 is 1, 0. Now, just like in decimal, I can add as many 0s to the front of the number as I want, and it won't change the value of the number. So really what I have at the very left end is 1 plus 0, which is just 1. And as we expected, 1 with 3 zeros after it is the decimal number 8, and that's how arithmetic works in binary. There's a lot more detail in section 1.5 of the book, and I would encourage you to read that as soon as possible this week, certainly before Friday. You will not be expected to be able to do multiplication in binary like the last example uses, and you don't need to worry about fractions in binary either. But go ahead and skim through those sections so you get the main idea of how it's actually done. Now, so far the only examples I've given you are 4-bit numbers or really small numbers, and most of the numbers we'll look at in binary are rather large. If I get a very large sequence like 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, and so forth, I can write as big of a number I want. It's actually really hard to say those numbers out loud or to talk to other people about them or even to write down the correct number if you're looking in the book and wanting to copy it on paper. So there's another system for writing binary numbers, and it's called hexadecimal. The word hex means 6, so hexadecimal is base 16. And the way hex works is we have the digits 0 through 9, just like in decimal, but then we need to have six additional digits that represent the numbers 11, 12, 13, and so forth. So the values actually go from 0 to 9 followed by a through f, where the value a means 10, and the value f means 15. You should take a look at the hexadecimal table on page 25 of the textbook, and this really is something you should memorize, but you don't have to memorize each set of numbers. There's a pattern to it, right? Basically, each hexadecimal digit corresponds to four bits of binary. The reason why there's four is that's how many bits it takes to represent 16 possible numbers. So for example, the value 0 is all zeros. The value 5 is 0, 1, 0, 1. The value 10, as we saw on the index card, is 1, 0, 1, 0. And the value f is 1, 1, 1, 1. So this makes a nice pattern for compressing long strings of binary numbers. All you have to do is divide up the binary numbers into chunks of four, and then figure out which four-bit number that represents an hexadecimal. So in this example I have here, 1, 1, 0, 1 is the hexadecimal value. I'll look it up in the table. D, 0, 1, 0, 1. Of course, I don't have to look this up in the table. I'm playing like I don't know what I'm doing, right? 1, 0, 1, 0 is the number 5. The value 1, 1, 0, 0 is the value 12. Now 12 doesn't exist in hexadecimal. That's two digits. So we use the letter c to represent the value 12. And finally, I have to add an extra 0 on the left end so that I have four bits, 0, 1, 1, 0. And that value is 6. So this long binary sequence in hexadecimal is simply 6C5D. And because all of these letters and numbers have a different pronunciation, it's a lot easier to communicate a long binary value to another person, whether you write it down or they hear you say it. It's a lot better than 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1. People are like, wait a minute, what was the 12th digit again? So that's the whole point of hexadecimal. This makes it easier to communicate long binary sequences.