 In this video we are going to explain the techniques that computers use to apply base encoding. More precisely, we are going to be talking about four types of base encoding. Base 10, which is what we are used to, base 2 or binary, which is what computers use, but then typically when we talk about computer systems, we usually handle encodings in base 8 and base 16. Let's start first with a simple example on how to make a change of base between base 10 and base 2. Let's take the number 326. 326 is a number represented in base 10. How do we translate this number between base 10 and base 2? The technique is called successive divisions. So what you do is you keep dividing it by 2 repeatedly. If we divide 326 by 2, we obtain the result. 163 is the quotient. In other words, it's 163 plus 0, the reminder. This reminder here is very important because it's going to be a bit of the result. More precisely, it's going to be the least significant bit of the result. So what we do is now this new number, 163, we keep dividing it by 2. 163 divided by 2 is 81 times 2, but in this case plus 1. We take 81 divided by 2 and it's 40 times 2 plus 1. Again, this value over here is the quotient, but the one we care about is this reminder that keeps appearing as we perform the successive divisions. If we keep dividing, 40 divided by 2 is equal to 20 times 2 plus 1. 20 divided by 2 is 10 times 2. Sorry, mistake. There's a 0 here. 10 times 2 plus 0. 10 divided by 2 is 5 times 2 plus 0. Now 5 divided by 2 is 2 times 2 plus 1. We have 1 as a remainder. And here is when we finish the procedure because 2 divided by 2 is equal to 1 times 2 plus 0. And 1 divided by 2 is 0 times 2 plus 1. So again, the important result here with these successive divisions, which by the way will always reach a 0 here, is that we obtain a series of remainders. And this is precisely the sequence of bit that is going to encode the number 326 in base 10 into binary. The thing to remember though is that the order in which we obtain these remainders is from least significant bit to most significant bit. And therefore the number has to be written starting from the bottom all the way to the top. So 326 in base 10 is 101000110 in base 2. So we now have shown how to make this transition. And obtain the number here 101000110. So this is the path to go from here to here. How do we reverse this translation? In other words, if we have the number again 10100110 and we want to translate it to base 10. The trick here is to realize that each of these bits here represents a power of the base. The bit over here has the weight 2 to the 0. This one has 2 to the 1. I'm talking about the weight, not the value that they contribute to the number. Any digit in this position has the weights that I'm drawing here, 2 to the 3. This one has 2 to the 4. 2 to the 5th for this one. 2 to the 6th for this one. 2 to the 7th for this one. And 2 to the 8th. Now the trick here is if this bit is 0, even though it has the weight 2 to the 0, it doesn't count because we have to multiply this weight to the digit. So the trick is only consider those digits that have value 1. And take them into account. This would be weight 1, 2, 4, 8, 16, 32, 64, 128, and 256. So this number that we've given here, we only need to worry about these two digits, which are weight 2 and 4. So it would be 2 plus 4. Then this digit over here, which we see has weight 64 plus 64. And then this one at the end, which has 256. So again, once we write these powers with these values, we only add those that have the corresponding digit equal to 1. In this encoding here, we have 1, 2, 3, 4 digits with weights 2, 4, 64, and 256. So if we perform this addition, we'll obtain again the number 326. So we just saw that we can now translate from base 2 to base 10. So these are the two methods that are used to work with base 10 and base 2. However, when we talk about hardware systems, we typically use another encoding, which is called actually octal. And the observation is the following. Let's take again the binary number, 1, 0, 1, 0, 0, 0, 1, 1, 0. If we wanted to translate this to base 8 directly from base 2, we could apply a technique similar to the one I've shown here, which is perform successive divisions by the new base. In this case, it would be 8. However, binary numbers have this property that if I remove this digit here, the least significant bit, and consider this one as the quotient, this would be the equivalent of dividing by 2. If I divide by 2, I can ignore this as the remainder, and this would be the quotient. Dividing by 4 is actually the same, but taking these two bits as the remainder, and these bits over here would be the quotient. And if I take three bits from the least significant, remember, always from the least significant bit, then this is the equivalent as divide by 8. And divide by 8 is precisely what we need to do to move from base 2 to base 8. So if I take this sequence of bits in base 2, and I want to move to base 8, I have to perform successive divisions. But successive divisions means that I take these three digits over here. This is the remainder of my first division. Now, if you look at these three digits, it's 0, 1, 1. If I look at 0, 1, 1 here, this binary number would be obtained by taking 2 plus 4. This is number 6, so I can rewrite this as number 6. And these bits over here are the remaining quotient. If I divide by 8 again, then I take another three bits. This would be the quotient, my new quotient. I'm doing exactly the same as we did here, successive divisions. Now, this division produces this quotient and this remainder, but this remainder is number 0. And these three bits here is the new quotient. If I divide it by 8 again, they will make the entire reminder. And as we can see, 1, 0, 1 would be the binary number that corresponds when I add 1, not a 2, and a 4. 4 plus 1 is 5. So there we have it. 5, 0, 6 in base 8 is the same as 1, 0, 1, 0, 0, 0, 1, 1, 0 in base 2 and itself is equal to 326 in base 10. So we can write here this result 5, 0, 6. Typically, when we handle these four bases, the way to distinguish an octal number from a base 10 number is by writing it with the prefix 0. So we would write 0, 5, 0, 6. And this 0 over here would tell us that we're talking about a number represented in octal. Now, suppose that I want to take the same number. 1, 0, 1, 0, 0, 0, 1, 1, 0. And now I want to perform successive divisions by 16. This would be equally trivial. Instead of taking three least significant bits as the remainder and the other bits as the quotient, since I'm dividing by 16, the trick is to take these four bits and make it a reminder. Now, dividing by 16 successively would be the equivalent of translating a number from base 2 to base 16. The problem with base 16 is that we don't have enough digits, right? We only have 10 digits from 0 to 9. If we want to work on base 16, we would need 16 digits. But here's the final trick. What we do is we use the 10 digits, and then we add A, B, C, D, E, and F as the six remaining digits we need to work with a base with 16 symbols, 16 digits. So now, as if we were counting, and instead of counting from 0 to 9 and then 10, we count from 0 to 9, then after 9 is A, B, C, D, E, F, and then after F, we would go to number 10. So once we solve this problem, if we want to encode a binary number in base 16, we apply exactly the same trick that we applied here with base 8, except that now we take groups of 4 rather than groups of 3. Here's the first digit we are considering, and as we can see, this binary number again is 0, 1, 1, 0 corresponds with the decimal number that I would obtain if I do 2 plus 4. Therefore, it's a 6. If I group another 4 digits here, that would be the next remainder of my successive divisions, then I would obtain the number 4. And then I'm left with one single bit here, so if I consider 5 tissue zeros here, up to the point where I make a group of 4, then I would obtain the number 146. It turns out that 146 is the hexadecimal representation of 1, 0, 1, 0, 0, 0, 1, 1, 0 in binary or 326 in decimal. Now, again, we need another trick to distinguish these numbers. We write them in different bases. And typically for hexadecimal, the trick we use is we put the prefix 0x in front of it. So 0x146 with this trick corresponds to the hexadecimal or base 16 representation of 1, 0, 1, 0, 0, 0, 1, 1, 0. Now, the only two things that remain to be shown is how do we go back? How do we actually start from an octal number and obtain its base 2 representation? But this is trivial because if we're given 0, 5, 0, 6, we know that it has a little in the left. Therefore, it's octal representation. And what we do is simply translate each one of the digits in its corresponding 3-bit binary representation. So the number 5 in binary is obviously 1, 0, 1. The number 0 is 3, 0, 0, 0, 0. Remember, always 3 digits for octal conversion. And then number 6 we know is 1, 1, 0. So we expand digit to 3 bits. And therefore, we have shown how to make this translation. Now, this is for octal. For hexadecimal, the trick is exactly the same. If we're given the number 0x146, what we do is expand this digit to 4 bits. In this case, it would be 0, 0, 0, 1. Number 4 would be 0, 1, 0, 0. And number 6 would be 0, 1, 1, 0. So what we're doing here is expand digit to 4 bits. In that way, we have shown how to translate hexadecimal to binary. Now, one final comment. Why do we use octal or hexadecimal? These two bases are inconvenient because they can be used as an abbreviation of binary numbers. Why? Because if I am given a very long binary number, instead of writing all the binary digits, it could be very long. What I can do directly is I group them in groups of 4 and immediately I write my number in hexadecimal. So for example, it's very common in hardware texts to see things like this is the value stored in memory. This is a way of providing a very quick abbreviation of a binary number that has 1, 2, 3, 4, 5, 6 times 4, 24 bits. And the corresponding value with 0s and 1s is very easily obtained applying the technique described here.