• Computer Science

Binary Number Examples

From the class:  The Binary Number System

Let's look at a couple more examples of binary numbers so that you can get the hang of it. And I want to talk to you about some of the properties of binary numbers that should be useful to you.

The first one to look at is because we can only have two symbols up here, so either this is going to be as 0 or it's going to be a 1. And if we multiply 0 by anything, it's just 0. And if we multiply 1 by anything, it's just the other thing. In this case, it's going to be powers of 2.

So in binary numbers, what we're really doing is just adding up different powers of 2 to get to the particular number we want to get to. So for instance, if we want to get to the number 2, we just need one power of 2, 2 to the first power, and that gets us immediately to the number 2.

So let's use that thought process to do a couple of basic examples. First, to represent the quantity 0 is pretty simple. We just put 0s into all the slots. And take note here that we have eight slots. And so I'm going to say over here that there's this variable called n equals 8. So we've got eight slots.

And remember that a slot when we're working with binary is called bits. And so this is an 8-bit number. And that's also called a byte. So we're working with one byte. One byte. OK, so we can just put 0 into each one of these slots and that will equal the number 0.

Now, let's say we want to get to the number 1. Well, the number 1 is very conveniently 2 to the 0 power. 2 to the 0 power is 1. Anything to the 0 power is 1. So what we can do is-- let me clean this up-- is just flip this bit here from 0 to 1. And that'll give us the number 1. So that's going to be 1 times 2 to the 0th power, which is equal to 1 times 1, which is equal to 1.

Now, let's say we wanted to get to 2. Well, if we want to get to 2, we don't need this 1 anymore. So we'll get rid of that. And we'll make that a 0. And we go to the next slot, which is 2 to the first power, which is conveniently 2. So we can just flip this bit to 1.

And we can represent 2 by saying binary 1, 0. So now this is going to be we're working right to left this time instead of the other way. So 0 times 2 to the 0 is going to be 0. Plus 1 times 2 to the first power, which is going to be 1 times 2, which is 2. So this is going to give us the number 2.

OK, let's say we want to get to 3, which is an odd number. Well, one of the properties of numbers is that to get to an odd number, if we add 1 to any even number, that gives us the odd number. So let me just clean this up. And all we need to do is to add 1 to any of the even numbers and we'll get the odd number.

So to get to 3, we just need to add 1 to 2 and that gives us 3. So to do that, we can keep the 2. But this time instead of this being a 0, we'll flip this bit to a 1. And now what we have is 1 times 2 to the 0th power plus 1 times 2 to the first power, which is equal to 1 plus 2, which is equal to 3.

All right, let's just keep going here and add one more bit to the left. And so this time, I'm going to turn on the bit in the third spot. And so now we're going to have three 1s. And if we do that, that just adds to this one more line, which is going to be 1 times 2 squared, which is 1 times 4, which is 4.

So now with these three numbers, 1, 1, 1, or these three bits, we've got the number 7. So one of things to note here is how many bits does it take to encode a particular number? We have eight bits total here, but what you're seeing is that to represent the number 7, we only need three bits of information. And to represent the number 1, we only need one bit. And to represent the number 2 or 3, we need two bits, and so on.

OK, so, so far these are pretty straightforward examples. You can see that what we're really just doing is deciding which one of these columns we're going to put a 1 in and then multiplying it by that power of 2 and then getting to our number that way.

Let's try a larger number to get the hang of that. So, so far we've been working with these sort of lower end bits. And let me reset these to 0. Let's work with a number that is large. Let's say 135. So I'm going to set these back to 0. And let's work with the number 135.

Well, let me show you a trick. What you can do to figure this out is to go to the highest number here in this list. So go to the highest power of 2 that doesn't go over the number 135. So we'll go to 128. Not 256 because that would put us over. But 128 is the highest number we can go to that's still less than 135.

And we're going to go and we're going to flip that bit to 1. So that's going to be the last bit in our number here. We're going to flip that to 1. Now, that's going to give us 128. So if we take 135 and we subtract from that 128, that leaves us with 7, which is what we need to get to. So we need to somehow add up to 7 with the rest of our bits.

And if you look over on the right, you can kind of just see the answer by looking at the numbers that we can add together here to get there. So 4 plus 2 is 6 plus 1 is 7. So we just need to flip this bit, this bit, and this bit to 1. So we'll come over here and the first three bits we'll just flip to 1s. So 1, 1, and 1. So we just represented the number 135 with 10000111. And so it took as eight full bits to represent this number.

So let me ask one final question before we finish up this video. What is the largest number we can represent with eight bits? Or for that matter, with any number of bits. Well, the largest number is going to be if all of these positions are 1. So we don't have any 0s.

So we'll just say that it's 1. And let me switch back to the pen. So 1 and 1, 1, 1, 1. And what we could do is just go through and add all these powers of two together from 0 all the way up to 7. So 1 plus 2 plus 4 plus 8 plus 16. And that would give us our number. But there's a nice math trick we can use to handle this summation. It's a little formula.

And what we do is we take the number of symbols that we have, in this case it's going to be 2 because we're working in binary, to the power of the number of slots that we have. In this case, it's eight. And then we're going to subtract 1, and that's because we're starting at position 0 here. So 2 to the eighth minus 1 is going to equal 256 minus 1, which is going to equal 255. So 255 is the largest number that we can represent with eight bits or eight spaces of information.