Transcript
• Computer Science

# The Hex Base (Base 16)

From the class:  The Binary Number System

There's another base that you're going to see often in computing called base 16. Base 16 or hexadecimal. Hexi for 16 or hex for short. So this is going to be called hex. And you'll see examples of this all over the place. CSS, for example, if you're used to working with CSS colors when writing your web applications, those are written in hex. Or if you've ever looked at C code that particular memory address, oftentimes that's written in hex.

So to get a sense for this, let's write out the powers of 16. Because since it's a base 16 system, we're going to be representing each slot as a power of 16. And we'll start off at one, just like we did before. And then 16 to the first power, 16 to the second power, as far to the left as we want to go.

OK, so to write this out 16 to the 0 power is equal to 1, And 16 to the first power is equal to 16. And we'll only do a few of these, just so that you can get a sense of it. And 16 squared is 256. 16 cubed is equal to 4,096.

Now notice what's happening here is as we go up, the numbers are getting substantially larger much faster than they would if we were using a base 2 system and even faster than they worried if we were using a base 10 system. So instead of going up by a multiple of 10, we're going up by about a multiple or power of 16 so. 1 to 16 to 256 to 4,096. So this is growing very quickly.

Now what we can do here, let's just look at an example. We need 16 symbols. And so we're going to go 0 through 9. So 0, 1, 2, all the way up to 9. But then we're going to keep going to n minus 1 symbol. 16 minus 1 is 15.

Now, we can't just write 10 because that would be ambiguous. Because does it mean 1, 0 or does it mean 10 and how do we tell the difference? So instead we just start using letters so it goes zero, one, two all the way up to nine. And then A stands for 10 and B stands for 11. C for 12, and we go all the way up to 15. So F is 15.

So if you see the letter, F it means 15 if you see the letter E it means the quantity 14 and so on. So we've got a base 16 system with powers of 16 in the slots. And we have 16 symbols to work with, because we start at 0 and we go up to 15.

OK, so to get the sense of this, let's start off with our first slot here. Let's put an F in this slot. Well, what that means is F is 15. So we can say 15 times 16 to the power of 0, which is equal to 15.

And if we go into the next slot here, let's just pick another random number. I'll just say B this time. So we'll put B here. What does that mean? Well, B is equal to 11. And then we'll multiply that times 16 to the power of whichever slot we're in. So now we're in the second slot, so we'll put 16 to the first power in the slot minus 1. And that is equal to 11 times 16, which is equal to 176.

And just like before, we can add up these two slots here. And that becomes, let's see, 11, 7, 8, 9, 191. So let me just double check here. Right, 191. So we can get This number, 191, using two slots. And we can write it in hex as BF.

I've cleaned this up a little bit and I want to show you the relationship between a base 16 number and a base 2 number and why hex is pretty useful in computing. So to do that, let's pretend that we have a half a byte. So that's going to be four bits. One, two, three, four.

Now, what's the largest number we could represent with four bits? Well, it's going to be if all of these things are one. Or to do it quickly, we can just say 2 to the fourth power minus 1. So that's going to be 15 is going to be the largest number.

Now, to represent 15, we actually need four slots or four bits to represent that number. Let's represent this same number in base 16. Well, if we come over here, you can see that 15 is one of our characters here. It's character F. And we can multiply that by 16 to the 0th power, which is just 1. And 15 times 1 is just 15.

So to represent this same number in hex, we can just use one slot. It's the first slot. And we'll just put an F there. Which is 15 times 16 to the 0th power. So for every four bits, we just need one slot in hex to represent the same number.

Here's another example where we have eight bits. So this is going to be eight bits or one byte. So this starts at 2 the 0 and goes up 2 the seventh. And again, the largest number that we can store with eight bits is when all of these are 1's. And that's going to be the number 2 to the eighth minus 1, which is going to equal 255.

Now to represent the same number in hex, the largest number that we can store in eight bits or one bite, we only need two slots. So let me change the color. And in the first slot, which is going to be 16 to the 0th power, we can put the maximum amount here. I've just pre-computed this ahead of time.

But you can use the same procedure that we've been using before to figure out the answer. And 16 to the 0 slot times 15. So this is going to be 15 times 16 to the 0th power is just going to be equal to 15. Then the second slot, we'll put in F as well.

And now this time it's going to be 15 times 16 to the first power, which is going to be equal to, I think, 240. And then when we add that up, we get 5 plus 0 is 5 plus 4 plus 1 is 5 plus 2. So it's going to equal 255.

So you can see that we're decreasing the number of slots we need by four. So to represent eight, we just need two slots or 8 divided by 4. So hex allows us to communicate these large memory addresses or large kind of bit patterns much more easily and concisely. And so that's why it's useful in computing.