 So, we have this basic problem in computing. We want our computers to deal with information like images, audio, video, 3D models, and 3D animation, and simpler stuff like text and numbers. The problem is that if we're going to store such things in the computer, we're going to have to store them in the form of bits. A bit, as you likely know, is simply something which holds one of two states, and we conventionally represent those two states as the digits 0 and 1. So for instance, the flag on your mailbox, which goes up or down, can be thought of as a bit. At any moment in time, it's holding one of two states, either up or down, and we can either assign by convention up to be 0 and down to be 1, or the other way around. It's basically arbitrary which we assign to which. The important part is just that the bit should make a clear distinction between its two states. In computers, the actual physical mechanism we have for representing bits varies from one technology to another. For instance, memory chips use either capacitors or transistors, optical disks use microscopic grooves that are rather written by lasers, and floppy disks and hard drives use charges on a magnetically sensitive surface. A bit, though, really is an abstraction, not a tangible thing, just like say a word is an abstract thing distinct from the tangible word written on a piece of paper. It works out that the key to representing any kind of data as bits is to first use bits to represent numbers. So that's what we'll cover in this video. How do we represent numbers as bits? To represent numbers as bits, we first must understand the concept of a number system. A number system is a scheme for symbolically and verbally representing quantity. Now of course in a sense, you already understand a number system because you already use one, the system called decimal. Decimal though is just one number system, one we've settled upon for historical and largely arbitrary reasons. Let's step back and consider the simplest way to express quantity, what's called the unary system or more commonly, the tally system. In the tally system, you make a mark for each thing you're counting. So for instance, to represent the quantity one, you just make one mark. To represent the quantity two, you make two marks, for three marks make three marks, for four make four marks, and so on. By convention, we usually group the marks into groups of five, such that every fifth mark is written diagonally through a group of four. The obvious problem with the tally system is that it requires a lot of marks and a lot of space to express even moderately large values. For instance, if you wanted to write the quantity one million, you would be writing marks for weeks, and even if you finished, the end result would be totally impractical to read. In another possible scheme, we would devise a unique symbol for every single possible quantity. The immediate objection though is that there are of course an infinite number of quantities, so we couldn't actually do this. The other obvious objection is that even if we only went up to some large number, like say one thousand, it would require everyone who uses that system to learn one thousand distinct symbols, and that of course isn't practical. A much better solution is to use what's called positional notation. Positional notation represents arbitrarily large quantities using a finite set of symbols. The base ten system, a.k.a. the decimal system you're already familiar with, is in fact positional notation. It's the positional notation scheme which uses ten symbols. The ten symbols we use in the West are called Arabic numerals. Oddly though, in the Arab countries today, they use a different set of numerals sometimes called the Indian numerals, or sometimes called the East Arabic numerals, as opposed to the West Arabic numerals which we use. Whatever you call your symbols and whatever they look like, the important idea in positional notation is that they have an established order. The order of our numerals is of course zero, one, two, three, four, five, six, seven, eight, nine. But this order means is that if we are at say two, and we wish to express the quantity which is one higher, then we go to the next symbol in the sequence three. And then if we want to express one higher than that, we go to the next symbol four, and then five, and so on. We don't skip from two to seven, or go back to one, or whatever. We stick to the order. The question then is what happens when you get to the last symbol nine? How do you express one higher than that quantity? Well, what happens is that we loop back to the first symbol, nine goes to zero, and we cycle the digit placed to the left. So say to express the quantity one higher than three, nine, the nine cycles back around to zero, and the digit to its left cycles from three to four. Well, you may be wondering then what if there isn't any digit to the left when we cycle from nine to zero. The answer is that there's always a digit to the left, an infinite number of them in fact, and they all start out set to zero. For example, what we express as just nine can be expressed as zero, nine, or zero, zero, zero, zero, zero, zero, zero, zero, nine. We just usually leave those zeros implicit, and of course we can't write them all anyway, because there's an infinite number of them. So say if you have the quantity represented by the single digit nine, the way you express one higher is to cycle the nine back to zero, and cycle the digit to its left, the zero, cycle that digit to one. And this is the value we call in decimal ten. Note that in decimal, when you have a series of nines in the rightmost digit place, and you want to express one higher, you get a cascading effect. Here for example, when you express one higher than 86999, what happens is that the rightmost nine cycles from nine to zero, causing the digit to its left to cycle from nine to zero, causing the digit to its left to cycle from nine to zero, causing the digit to its left to the six to cycle to seven. So 86999 becomes 8700. You'll also note that English and other modern human languages presume decimal. We have a name for each of the ten Arabic normals, but then when those numerals are put in decimal digit places other than the rightmost one, we have other names for the numerals. For instance, we say 78, we don't say 78. The historical decision to use positional notation was not an arbitrary decision. Positional notation is clearly superior to these alternatives. However, the choice to use ten symbols is arbitrary. Positional notation can use any number of symbols as long as you have at least two. So say you could have a base three system, a base 17 system, or a base 1065 system, or whatever. The only real guiding logic in this choice is that you obviously wouldn't want a number base that's too big, because that would mean memorizing and using a lot of symbols. The decision to use base 10 was likely made simply because humans have 10 fingers and 10 toes, so it seems natural to people to use base 10. Historically, however, some civilizations like the ancient Babylonians actually used base 12, likely because it best fit with their calendar. For comparison, let's look at what happens when we use positional notation with a number of symbols other than 10. The system in which we use eight symbols is called base eight, or octal. In octal, we still use the Arabic numerals, but we ditch the symbols eight and nine. Consequently, the numeral seven cycles back down to zero. So to express one higher than the quantity seven, we cycle the seven back down to zero, and the digit to its left cycles to one. So the quantity in decimal, which we call eight, in octal is written as one zero. This is certainly confusing after a lifetime habit of using decimal. It's just very difficult to look at one zero and not immediately think 10. To make what's going on totally clear, here we're counting in both decimal and octal side by side. As you can see, the quantity which we call 12 in decimal is in octal expressed with the digits one four. And if we continue counting, you can see here that the quantity which we call 24 in decimal in octals written three zero. Again, the way we count in octal is the same as in decimal, we just use eight symbols instead of 10. And whereas in decimal, we saw the cascading effect when the rightmost digits of a number were nine. In octal, you see the same effect when the rightmost digits are seven, because seven is the last symbol in the sequence of octal. For example, when we express one higher than the quantity three, one, seven, seven, seven, seven, we get three, two, zero, zero, zero. What happens is that first the rightmost digit cycles by one, so it cycles from seven, the last numeral, back down to zero, the first numeral. Because we cycle this digit from the last numeral to the first, we cycle the digit two to the left as well, and this effect cascades until lastly the one cycles to two. Because octal uses the same numerals we use in decimal, we need some way to distinguish numbers which are meant to be octal. There are a number of conventions for doing this, but the most common is to proceed an octal number with the leading zero digit, e.g. the leading zero of zero, six, one indicates that the number is meant to be an octal number. To be honest, this is a pretty terrible convention because a leading zero doesn't reliably indicate that a number is meant to be octal. Sometimes we write leading zeros in front of decimal numbers just because we feel like it. So understand that this convention is not a hard and fast rule. As per saying numbers allowed, when reading non-decimal numbers, the rule is simple. Always state the number base explicitly and simply read each individual digit. So when saying this octal number allowed, I should say either 6-1 octal or octal 6-1. What I should never do is read the digits as 61 because it's very hard to hear or say 61 and not think in decimal. It's confusing enough to think in number bases other than 10, so best to avoid the built-in English bias for base 10. For another example, instead of saying 5,673 octal, I should say 5673 octal. So octal is an example of positional notation with a number base less than 10, but what about a number base greater than 10? One such number base we might use is base 16, also commonly known as hexadecimal or hex for short. Because hex uses more than 10 symbols, we can't just use the standard Arabic numerals, but rather than contriving new symbols, we just use the same Arabic numerals as we do in decimal and tack on the first six letters of the alphabet. So the symbols in hex are in order 0,1,2,3,4,5,6,7,8,9, A,B,C,D,E,F. Whether these letters are written uppercase or lowercase doesn't matter, you'll see them written both ways. Once again, the idea in positional notation is that the last symbol in the sequence cycles back to the first. So because F is the last symbol in the sequence, when we cycle digits in hex, we cycle from F back down to zero. So whereas in octal, we had the cascading effect when the rightmost digits were seven, in hex we get this effect when the rightmost digits are Fs. Here then is counting in hex. The quantity which we write one zero and call 10 in decimal is just written A in hex. And it's not until we get to the quantity which we call 16 in decimal that we begin to use more than just one digit. 16 decimal is equal to one zero hex. Here we see the pattern continue. Notice for instance that the quantity two zero hex is equivalent to 32 decimal. When we write numbers in hex, we have the same issue as we have in octal. A reader may not be able to discern just from the numerals that the intended number base is something other than decimal. In hex though, we also have this issue that some hex numbers actually look like words because a hex number might be made up entirely of the letters A through F. So to denote hex numbers, one common convention is to proceed hex numbers with zero X, where the X can be either upper case or lower case. The non decimal number base we care most about is base two, also called binary. In binary, we just use the two symbols zero and one and that should tell you why we care about binary. If we can represent quantities using just two symbols, then we can represent quantities as bits. Once again, the last symbol in the sequence, this time one, goes back down to the first symbol zero when cycled. So to express one greater than zero one, the one cycles back down to zero and then the digit to its left cycles from zero to one. Hence when counting, we start off with one binary equal in one decimal, one zero binary equal in two decimal, one one binary equal in three decimal, one zero zero binary equal in four decimal, and so on. The striking thing here is that because we're using so few symbols in binary just two, we end up using many digit places very quickly even to represent small quantities. For instance, as you can see, the quantity eight in decimal is equal to one zero zero binary. We've only gotten to the quantity eight, but we're already using four digits. The quantity 16 in decimal is equal to one zero zero zero zero binary. We've only gotten to the quantity 16, but we're already using five digits. And again, whereas in decimal, we see the cascading effect when the rightmost digits are nines and in octa we see the effect when the rightmost digits are sevens and in hex we see the effect when the rightmost digits are f's. In binary, one is the last symbol in the sequence. So we see the cascading effect when the rightmost digits are ones. Here, for example, one higher than the quantity one zero one zero one one one one binary is one zero one one zero zero zero binary because the rightmost ones cycle to zero and the first zero from the right cycles to one. To distinguish a written binary number from a written decimal number, we typically just stick a B at the end of the number either uppercase or lowercase. Though I generally find lowercase B stands out better because it doesn't look like an eight. You should understand now how to count in these different number bases, but what about converting a quantity from one number base to another? To understand how to do this, we first need to consider a different way of thinking about positional notation. Say I have this decimal number 36,259. This is equivalent to 30,000 plus 6,000 plus 200 plus 50 plus nine. In other words, we can think of the whole as a sum of individual digit places. We can go a step further and say that this is in turn equal to the quantity three times 10,000 plus six times 1,000 plus two times 100 plus five times 10 plus nine times one. And we can go even another step further and say that this is equivalent to three times 10 to the fourth plus six times 10 to the third plus two times 10 to the second plus five times 10 to the first plus nine times 10 to the zeroth power. Recall that anything to the zeroth power is equal to one, so 10 to the zeroth is one. What we've done here is given the explanation for positional notation, which mathematicians prefer. In this explanation, each digit place represents that digit multiplied by a power of the number base and these powers increase from the right. So in decimal, because our number base is 10, we start on the right with 10 to the zeroth, which is one, then 10 to the first, which is 10, then 10 to the second, which is 100, then 10 to the third, which is 1,000, then 10 to the fourth, which is 10,000, and so on. Applying this same idea to octal, the octal number 3675 is equivalent to octal 3,000 plus octal 6,00 plus octal 70 plus octal five. In the next step, we factor out the digits, so say octal 6,00 is equivalent to octal six times octal 1,00. Next, we can express these round octal numbers in terms of powers of the number base, e.g. 1,0 octal raised to the third is 1,000 octal. Note that 1,0 is always the number base no matter what the number base. In octal, 1,0 is the decimal quantity eight, in hex, 1,0 is the decimal quantity 16, in binary, 1,0 is the decimal quantity two, and in decimal, 1,0 is the decimal quantity 10. Now that we've broken our large octal number down into small octal numbers, we can convert those small numbers to decimal. Then we can simply do the arithmetic to get a single decimal number, the equivalent of our original number octal three, six, seven, five. First, we do the exponents, then the multiplication, and then sum everything together, yielding 1981. This technique, composing a large number into smaller numbers, converting the small numbers to a different number base, and then reassembling can be used to convert from any number base to any other. However, the trouble is that when converting to any number base other than decimal, you end up having to do a arithmetic in that number base, which is doable, but easily confusing. Consider converting this decimal number 36,256 to octal. First, we decompose it as we've done before, so we get three times 10 to the fourth, six times 10 to the third, two times 10 to the second, five times 10 to the first, and six times 10 to the zero. After converting these numbers to octal, we then have to do the arithmetic in octal. Multiplication and addition work just the same in octal as in decimal, except you must keep in mind that seven is the last numeral when carrying digits. I'll alight over the grunt work here. What we end up with is octal three times octal 23420 plus octal six times octal 1750 plus octal two times octal 144 plus octal five times octal 12 plus octal six times octal one. After the multiplication, we have octal 72460 plus octal 13560 plus octal 310 plus octal 62 plus octal six, which all adds up to octal 106640. For another example, we'll convert hex three, six, E59 to decimal. Again, we first decompose into smaller numbers and then convert them to decimal. Then we do the exponents, do the multiplication, and then sum everything together, yielding here decimal 224,857. Applying the same technique for converting binary to decimal, we first decompose the digits and convert the small terms to decimal. Then we do the powers, multiply, and sum everything together to get decimal 21. Notice though that we ended up adding together only powers of two. This suggests a mental shortcut. Each binary digit place effectively represents a power of two. When converting to decimal, each power of two gets multiplied by either one or zero. So effectively we end up simply adding together the powers of two for each digit place that has a one. So for example, for binary 10101101, we add together 128, 32, 8, 4, and 1, yielding 173. When converting the other way from decimal to binary, the mental shortcut is to find the powers of two that add up to our decimal number. The binary number then has ones in all the digit places that correspond to those powers of two. To find these powers of two, we first find the biggest that fits in our decimal number, subtract it out, and then repeat the process until eventually the subtraction yields zero. For example, converting decimal 35,872 to binary, the largest power of two that fits is two to the 15th, 32,768. Once we then subtract out 32,768, we're left with 3,104, in which the largest power of two that fits is two to the 11th, 2048. Subtracting out 2048 leaves us with 1056, in which the largest power of two that fits is two to the 10th, 1024. Subtracting out 1024 leaves us with 32, in which the largest power of two that fits is 32 itself. Subtracting out 32, of course, leaves us with zero, so we found all the powers of two. The component powers of two we identified were two to the 15th, two to the 11th, two to the 10th, and two to the fifth. So our binary number has ones in those four digit places and zeros in all the rest. Partly for the sake of conversions to and from binary, it's generally a good idea for a programmer to memorize the powers of two up to about two to the 20th or maybe up to two to the 32nd. Go to high though and the numbers and the arithmetic become too unwieldy to compute mentally for most people, so they're generally not worth remembering.