welcome to exploring computing today's video is bits bytes and binary part two or one plus one equals one zero so in the last lecture we took a look at binary numbers and why computers use the binary number system in this lecture we're going to take a closer look at how binary numbers actually work now i do want to start off by saying we are going to be taking a look at a bit of math here in this particular video and i don't want people to think that this is what the bulk of this class is going to look like this is largely the only video in which we're going to be looking at math but i do think that having a sense of how these binary numbers really work and getting a sense for when you see a binary number what it actually might represent is going to be pretty useful as things go on we're going to see that the binary number system pervades computers and that there are many many places where the fact that the underlying representation of data in the computer that uses these binary numbers is going to leak out into the real world let's go ahead and take a look at this so let's start off by learning how to count now i know that seems like an odd place to start off a lecture at stanford university but bear with me we're going to review how to count decimal and this will turn out to be useful when we're trying to understand how counting in binary works so i'm going to start off with a 0 and i'm going to add 1 to it and of course that's going to give us 1. we're going to add 1 to 1 and that will give us two we'll add two to one and that'll give us three and so on and then we'll continue on we'll get to eight we'll add one to eight that'll give us nine we'll add one to nine and that's where things get a little bit dicey because remember we're working with the decimal number system there are only ten digits starting with zero going up to nine and so what happens when we try and add one to nine is there's no eleventh digit so what happens is we carry the one and we reset our ones to zero and we end up with one zero or which is of course ten now we can continue on so ten plus one is eleven eleven plus one is twelve and so on now when we get to nineteen plus one well this is similar to what happened with a nine plus one we know that we can't go to one some digit greater than nine because there are only ten digits zero through nine and so again we carry the one over and we end up with 20. this will continue on we get 98 plus one equals 99 we get 99 plus one and again we have this situation where neither the tens digits nor the ones digit can go any higher we only have ten digits so we carry over from the ones to the tens and from the tens to the next digit over which will be the hundreds and so we end up with a hundred okay fair enough let's take a look at how this works in binary now so in binary we're going to start off with zero plus one again just as we did in decimal zero plus one in binary is equal to one just as zero plus one and decimal is equal to one but as soon as we get to the next step we run into a problem what does one plus one equal in binary well this is analogous to the situation we got to before when we tried to add 1 to 9 in the decimal number system the decimal number system we had those 10 individual digits in the binary number system we only have two digits 0 and 1. so when we add 1 to 1 there is no third digit there's only 0 and 1 and so we need to go ahead and carry the 1 over so this will actually give us 1 0. we can go ahead and add 1 to 1 0 which will give us 1 1 when we try and add 1 to 1 1 this is similar to the situation where we tried to add 1 to 99 we need to carry the 1 over and so we're going to get 1 0 0. we can keep on going with this so 1 0 0 plus 1 equals 101 we can keep going and continue on and so what i've written up here is the first eight numbers of the binary number system are actually the first nine of you consider zero is the first number in the binary number system so 0 1 1 0 1 1 1 0 0 101 1 1 0 1 1 1 and finally 1 0 0 0. so here are the first 16 numbers in the binary number system and you can see that they are the dust the equivalent of the decimal numbers 0 through 15. you'll also notice that i've got these little subscripts next to each of the numbers so these subscripts are sometimes used to disambiguate when it's not clear what particular number system a number is using so in this example here i've got the number 6 on the left but i want to emphasize that it's a decimal number so that 10 in subscript after the 6 represents that that 6 on the left side here is base 10 and the 2 on the that's a subscript on the right represents that that number on the right is base 2. now without it without that subscript 10 and without that scope subscript 2 you would probably guess that the number on the left is base 10 because it's clearly not binary it's got it's got a digit there 6 which does not exist in the binary number system although there are some other number systems like the base 8 number system which occasionally gets used in computer science and then there's a base 16 number system called hexadecimal which does get used quite a bit in computer science we'll look at that in another lecture and so that six on the left could be either one of those number systems and so putting that 10 down there at the bottom emphasizes that this is in fact a decimal number and on the right 1 1 0 this could represent 110 in base 10 and so that little subscript there too tells us that no this is not 110 this is actually 1 1 0. which does kind of bring us to the next question which is how are we working out the equivalencies of these different numbers so let's take a very short look at this and i'm not going to require you guys to be able to do the conversion from binary to decimal you know at any point in the quarter and this is the point where i would normally say we're not going to make you do it on an exam but hey because we're under quarantine there are no exams so uh go coronavirus i guess but i do want you to to have a sense of how it works so again we're going to start with a decimal number because this is a system we're used to and by reminding ourselves of how the system actually works will help us when we try and convert it to binary so here i've got the number 1891. now if you think about what does 1891 actually represent we've got four different digits here the one on the far right represents 10 to the zeroth power which is our ones the num the second number over the 9 represents our 2 to the first power or our tens the eight represents 10 to the second power or hundreds and the fourth number over represents our third powers are 10 to the third which is a thousand so if i'm looking at 1891 i know i have 1 10 to the 0th power i have 9 10 to the first powers 8 10 to the second powers and 1 10 to the third power so i have 1 plus 90 plus 800 plus a thousand which gives us 1891. let's go ahead and take a look at how this works with binary numbers so here i've got the binary number 1 1 0 1. what is the decimal equivalent of this well on the far right in the zeroth place i have my two to the zeros so these this these are one because two to the zero power is one the next place over represents my two to the first powers so those represent our twos next to that to the left i have our two to the second powers which represent fours and then one step over from that i have our two to the third powers which are eights in this particular case i have one two to the zero power i have no two to the first powers i have 1 2 to the second power and i have 1 2 to the third power so i have 1 plus 0 plus 4 plus 8 which means that this is the equivalent of a decimal 13. so one thing that's going to be important for us when we're representing information is how many different combinations can we store in a given number of bits so let's start off with one bit what can we represent in a single bit well we can either represent a zero or a one what is that actually good for the most common thing that we can use a zero or a one to represent is actually whether something is true or false so are we going to be able to come back to stanford for summer session well if that bit is stirring a 1 then the answer is yes and if that bit of storing is 0 then the answer is no how many different combinations can we store in two bits well here are the different combinations two bits and you can see that there are four possible combinations how about the combinations in three bits here are the different combinations in three bits and then finally here are the combinations in four bits so the way to think of these is you can either think of them as oh those are binary numbers and we saw how to convert those to decimal a minute ago so you know we have these 16 possible combinations these could represent the decimal numbers between 0 and 15 but they might be used to represent something else so for example i could i could use these 16 combinations to represent the ten provinces in canada so if we were a school in canada and we wanted to know which province somebody lived in i could have zero zero zero zero for example represent prince edward island and zero zero zero one nova scotia 0010 representing new brunswick and so on could we represent information about u.s states and our four bids no we can't there's 50 different states and we can only store up to 16 different combinations in our four bits so well our canadian provinces fit into four bit and in fact we could comfortably add in the three canadian territories as well we simply do not have enough space in four bits to store the us states and in fact if we had five bits we still couldn't store it we're going to see that we actually need six bits to store all the states the union so we've seen that in one bit we can represent two combinations and two bits we can represent four three eight four sixteen and it turns out there's a simple formula here if we have n bits we can represent two to the n combinations and so here you can see the common number of combinations we can have for one two three four five six seven eight nine and ten bits now when you're looking at these different combinations keep in mind that these individual bits could represent numbers they can represent positive and negative numbers they can represent different things like our canadian provinces we could use a certain sequence of bits to represent dorms at stanford and so one of the things that the programmer is going to have to decide to do is what do they want to do with the individual bits when we're designing a program we're going to need to decide how many bits to set aside for what particular quantities and how we're going to represent things with bits and bytes and that's something that we're going to be exploring more later this lecture and throughout throughout the rest of this week now another thing that you might notice is if we're looking at these numbers here two four eight sixteen thirty two sixty four hundred twenty eight two fifty six five hundred twelve these may look a little bit familiar to you if you've gone shopping for a new say iphone recently how much memory do you buy in an iphone can you buy a hundred megabytes can you buy 250 megabytes no you buy 128 or 256 or 512 why because those are powers of two and because internally the computer uses the binary number system now we've only solved half the mystery though now we understand why we buy iphones with 64 128 or 256 but why do we buy them in megabytes or gigabytes instead of millions and billions well it turns out that millions billions thousands these are all powers of 10 and again internally we're using the binary number system not the decimal number system and so kilobytes megabytes gigabytes and terabytes are the base two equivalent and you can see from this table here that two to the tenth is about a thousand but it's actually a little bit bigger and two to the 20th which is a megabyte is about a million but it's actually a little bit bigger and the same thing for gigabytes and terabytes so the idea here is we're going to give you roughly the same amount as a billion or a million or a thousand but we want to do it in a power of two and you can see here some higher order numbers that you might run into someday peta exa zeta and yoda i should mention that there is some disagreement between the electoral engineers and the computer scientists on whether or not we should be using powers of two so there are places where when we're working with electrical engineers they will use kila mega or giga and they'll actually be real referring to thousands millions and billions in base 10. and so the most common place you might run into this is when you're talking about communication speeds those are generally actually given in powers of 10. all right in the next lecture we're going to take a look at what happens when things go wrong with our binary numbers