Binary Numbers (Khan Academy) - AP Computer Science Principles

Computer Science
96 followers
Hello and welcome back to AP Computer Science Principles. Today, we are going to go ahead and
jump back into Khan Academy and do a few example problems involving binary numbers. But first,
I would like to remind you very quickly that underneath each of these unit or topic titles,
there is a list of standards here, which is what that topic is supposed to be covering.
So it might be a good idea to just take a quick glance at those, make sure you know what it is
you're supposed to be learning. And then also recall, there is an article and several videos
here that you are definitely going to want to watch and probably take notes on. Okay, but for
us, we are going to jump into this binary numbers problem set. Let's go. Which of these lists
correctly orders the binary numbers from smallest to largest. So we want to know which of these
here, I'll scoot over a little bit, which of these lists, lists it from smallest to biggest.
Now, I don't want to have to translate 16, I think numbers into decimal, I would rather use
some patterns to try to figure out which one is in the correct order without having to actually
translate it into decimal. So what I'm going to do is I'm going to recall that any of the sort of
leftmost place values are very large, and the rightmost place values are usually very small.
So smaller numbers will have more numbers over here on the right, bigger numbers will have more
numbers here on the left. And since the only number that we can get other than zero is one,
we should be able to sort of eyeball which one is counting up. Fair warning though, this is a
particular kind of problem I miss all the time. So let's take a look. This first one,
just look at the first three here. Yeah? Just look at the first three, that's a 000 in front of a
001. That second number is larger than the third number, this is not our correct answer.
By the way, if you do want to just go ahead and translate these numbers to decimal and compare
them that way, that's totally valid. Okay, so for part B, just looking at the first three, it goes
000, 000, 001, and 010. So far, we're on the right track. It is increasing when it comes to the very
largest digits. Now let's look a little bit closer. The last five digits, 10001 and 10101.
That's definitely bigger, right? It's got another one than the first one. How about this next one?
001, we already know it's bigger. Yeah? The moment we have a one that is further left than
any of the ones in the other two, we have a larger number. Again, this is just like with
decimal systems, you're just not used to looking at it with only zeros and ones.
I believe B is the correct one. Very nice.
A bunch of computer scientists take over an island and start their own country.
They want the license plates to use binary numbers. There's space for seven digits on
each license plate, and the first plate starts at 000, 000, 000. How many unique license plates
can their country support? Let's go ahead and get rid of that X.
Okay, so this is a throwback to an earlier video. I will go ahead and do a quick review.
So if you have N bits, and remember N is just a symbol for some arbitrary number. It might be 5,
it might be 20, it might be a million. N bits. The highest number that we can make
was 2 to the N and then minus 1. This should be a review, but if it's not,
go ahead and put it in your notes. The largest number, so if you have 7 bits,
the biggest number you can make is 2 to the 7th minus 1. If you have 12 bits, 2 to the 12th minus
1. Now we do have 2 to the 7th, so if I recall properly, 2 to the 7th is, let's see, 2, 4, 8,
16, 32, 64, 128. Okay, 2 to the 7th is 128 minus 1 is 127. But, ha ha, I'm catching myself almost
messing up. It doesn't ask what the highest number they can make is. Read the question carefully.
It says how many license plates can they make. Yeah, they can make up to 127, but they can also
make, as it says in the problem, 0, 0, 0, 0, 0, 0. So there are actually 128 plates they can make,
including that one that's all zeros. Almost got tripped up by that one. Do be careful.
Read those problems carefully. Okay, problem 3. Your programmer friend told you that they
just turned 1, 1, 1, 1, 0 years old in binary. How old are they in decimal? This is a great chance
to review. We're going to go ahead and make 1, 2, 3, 4, 5 placeholders, and we're going to fill in
1, 1, 1, 1, 0. Excellent. Now, we've got a couple of ways that we can go about this. We can just
do a bunch of addition, or we can look at this number and realize that it is one less than the
biggest number we could possibly make with 5 bits. Yeah? The biggest number we could make with
1, 2, 3, 4, 5 place values would be all 1s. And we already know, as we just did a moment ago,
that the largest number we could make is 2 to the 1, 2, 3, 4, 5th minus 1. Yeah?
The number all 1s, 1, 1, 1, 1, 1, is 2 to the 5th, which is 32, minus 1 is 31. So, if this was all 1s,
yeah, this number would be 31. But it's not, of course. It's 1, 1, 1, 1, 0, so this number is
actually equal to 30 instead of 31. Now, we're going to go ahead and double check this against
the old method anyway, but it's really a good idea to keep your eyes open for shortcuts like this,
because on the AP exam, you often have a very limited amount of time. Okay, so the other way
we could solve this, we set up our place values, we label them accordingly. As always, we put the
rightmost, I'm saying it correctly this time, the rightmost bit in the 1s place, and then all of the
rest of them stack up to the left. So, we've got a 1 in the 16s place, and we have a 1 in the 8s place.
So, again, just to recap really fast, this means 1 times 16. If there's a 1 in there, that means
that 1 times 16 is part of that sum. I'm not going to write it any further than this, because it's
going to get really messy really fast, but we do have 1, 4. We have 1, 2, but we don't have a 1,
right? It's 0 times 1 equals 0. We add up all the things that we've got. 16 and 8 is 24, and 4 is 28,
and then 29, 30. So, whichever way you go, and whichever way you decide to justify your answer,
the answer is your programmer friend is 30 years old. You're visiting a computer science building
at a college, and they've decided it would be fun to display all the room numbers in binary.
You're going to room 13. What binary number should you look for on the door of room 13?
Okay, we're going to do this two different ways since it is the last problem. We learned in the
last video two distinct methods when it comes to taking a decimal number and turning it into
binary. We will start with the algorithm that I showed at the very end. Now, as a quick reminder,
the way that works is you take the number, you divide it by 2 with remainders. So, 13 divided by
2 is 6, with a remainder of 1, right? 6 times 2 is 12, plus 1 is 13. And then we continue the process.
6 divided by 2 is 3, with a remainder of 0. 3 divided by 2 is 1, with a remainder of 1. And
finally, 1 divided by 2 is 0, with a remainder of 1. And as a reminder, you read the binary number
from the remainders bottom to top. So, don't forget, if you write down this number top to
bottom, you're going to get it wrong. Read it bottom to top. The correct answer is 1101.
So, that is the method that we learned at the end of the video. I'm going to go ahead and do this
one more time. This is my preferred method, but again, when you're in my class, so long as you
justify what you're doing, I don't care which method you use. It's totally up to you. So, I'm
going to say, alright, so we've got the 1, 2, 4, 8 spaces, and I know I don't need a 5th bit, because
that 5th bit would be worth 16, and that's too many, right? I wouldn't be able to fit that 16
into the 13 I'm looking for, okay? So now, it is a, again, it's an addition game. Now, a couple of
sort of things that I do when I'm figuring this out. I always start with the highest number, and I
put a 1 there, yeah? I know that 8 is a smaller number than 13, so I take the 8 away from the 13.
Alright, so 8, 13 minus 8, 12, 11, 10, 9, 8, 7, 6, 5, okay? Then I move on to the next digit, 4.
Does that fit into what I have left? Well, yes, 4 fits into 5, so I'll put a 1 there. Now I've got
12 out of the 13 that I want. I have 1 left over. 2 is too many, so I put a 0 there. I can't put the 2
in, it would make the number too big. And then I put a 1 in the 1s place, and I get 8 and 4 and 1
is 13. Either way, the correct answer is 1, 1, 0, 1. And that's converting binary numbers. Up next,
we are going to talk a little bit about binary fractions, and then we'll talk a little bit about
yet another number system, hexadecimal, which is actually a base 16 system that is often used
especially with color palettes in things like Photoshop. Thanks for watching, I'll see you next time.
