fbpx

Hexadecimal gives you a better way to represent binary numbers. In one of the very early episodes, I explained how bytes are composed of eight bits. On some platforms, a byte might actually be more than eight bits. The real problem though is even eight bits are hard to read. The zeros and ones blend together. Hexadecimal allows you to work with four bits at a time and is much easier to read.

Here’s table of some common values:

Decimal Binary Hexadecimal
0
00000000
0x00
1
00000001
0x01
2
00000010
0x02
3
00000011
0x03
4
00000100
0x04
5
00000101
0x05
6
00000110
0x06
7
00000111
0x07
8
00001000
0x08
9
00001001
0x09
10
00001010
0x0a
11
00001011
0x0b
12
00001100
0x0c
13
00001101
0x0d
14
00001110
0x0e
15
00001111
0x0f
16
00010000
0x10
32
00100000
0x20
64
01000000
0x40
128
10000000
0x80
255
11111111
0xff

 

Listen to the full episode for more or you can also read the full transcript below.

Transcript

It doesn’t matter what number system you’re working with, well, except maybe Roman numerals which are just too difficult to add and subtract. The problem with Roman numerals is that there’s no placeholder system. So let me first clarify that it doesn’t matter what placeholder number system you’re working with. There is one rule that applies to them all. You need unique symbols that represent each digit starting with zero. However many digits you have will be based on the placeholder value system you’re using.

This is called the number system base. For our everyday counting, we use a base ten number system. However, our school system has failed us. It’s not really the fault of the school system really. This failure goes back much farther than any modern school system. You see, we were all taught to count wrong.

When counting on our fingers, we naturally start with one and go to ten. We have a base ten number system and we have ten fingers so it makes sense that when we hold up a single finger, that’s one and when we hold up all ten fingers, that’s ten.

This simple mistake in the way we count is probably the single biggest obstacle that prevents a lot of people from understanding different number systems.

Here’s what I’d like you to do. It’s also a very simple and small change. When you count, start from zero. Get in the habit of recognizing zero as a valid digit all by itself. Most people associate zero more with the value ten than with zero itself.

As you develop this way of thinking, you’ll find it comes easier and will prepare you for how most collections treat the first item in the collection to be at index zero.

It will also help you understand placeholder number systems. Because no matter what number system you use, they all start with zero. Our base ten number system has ten digits. These digits are zero through nine. They are most definitely not one through ten. Forget what you learned in Sesame Street and first grade. and remember that numbers start at zero.

Why is this so important that I’d spend the whole first half of this episode trying to convince you to start counting at zero? I’ll explain that right after this message from our sponsor.

( Message from Sponsor )

Going back to an eight bit byte, there are 256 possible values that range from 0 through 255. See, there’s that whole start from zero thing again. If we wanted to, we could assign a unique symbol for each of these values. That’s quite a daunting task. Could you easily memorize 256 squiggly shapes and how would we draw them anyway so they would be easily recognizable? We don’t even have a very good way of distinguishing the 26 letters in the English language. I mean really, just look at the difference between a capital O and a capital Q. It’s just one tiny tail of a mark that separates them. Imagine if we tried to come up with 256 different shapes. And the names! Oh my! What would we call all these values?

So that’s obviously too much. What if we cut the byte in half though? What if we treat each byte as if it was composed of two pieces that are each four bits in length? How many values can we get out of four bits? We can get 16 which range from, you guessed it, 0 through 15.

Now we already have some well known and named digits that range from zero through nine. These are perfectly good digits. They already have nice squiggly shapes that everybody knows and they already have names. So let’s use them. We only need six more symbols and names and we would have 16 unique squiggly shapes and names for each value that we can represent with 4 bits.

Let’s use some English letters! What? Letters aren’t for counting! They’re used for spelling words. That’s true and this decision could lead to some confusion. But the benefits of having symbols with well known names is just too good to pass. So we’ll use the first six letters a through f. It doesn’t matter if they’re uppercase or lowercase. All we’re using them for is because everybody knows what the letters a, b, c, d, e, and f look like and what their names are.

This is the reason I argued so hard for counting from zero through nine. Because it puts the pause at the right spot just before we need to advance to the next placeholder value. You see, in decimal, the number ten needs to use a one in the second place value.

But we want to be able to count all the way to 15 with just a single digit. That’s why we went through all the thought about what symbols to use. It’s also why I was calling the symbols squiggly shapes. We want 16 total digits that range from 0 through 15 without needing to make use of another place value.

This is the hexadecimal number system. It’s a base 16 number system. And it’s extremely popular and useful in programming because it aligns perfectly with half of a byte. By using the existing numeric digits plus six extra letters, we have our 16 single hexadecimal digits.

In hexadecimal, when you get to the number 9, you’re not done with that digit. You can keep counting higher because you have 6 more digits. the fact that these 6 extra digits just happen to look a lot like letters and that they have the same name as letters is there just to make the system more familiar.

So in hexadecimal, when you get to 9, the next number is not 10, it’s a. that’s right, a is a number now. The number a represents the same value a ten. If you have ten sheep in front of you, you can now say that you have a sheep. Add one more sheep and you now have b sheep. Then c, then d, then e, and finally if there are a total of 15 sheep in front of you, then you can say that you have f sheep.

What happens when you add another sheep? This gives you 16 sheep now in decimal. In hexadecimal, you say that you have one zero sheep. You’ve run out of digits and just like in decimal, what do you do when you reach the highest digit? You start using the next place holder. That’s why in decimal, 9 goes to 10 and it’s the same reason why in hexadecimal that f goes to one zero.

Now one zero looks a lot like ten. In fact, they look absolutely identical. I’m being careful to call this one zero in hexadecimal to at least try keeping them separate when talking. But what about when writing hexadecimal numbers?

How can you tell is the digits 1 and 0 represent ten or sixteen? Or for that matter, if this was binary, then the digits 1 and 0 would represent the value 2. How are we ever going to keep things straight?

It’s common when writing a hexadecimal number to prefix the number with 0x. So if you see something that says 0x10, then you know that’s hexadecimal and represents the value 16.

Because we have this system in place, any byte value can now be easily written with just two hexadecimal digits. A byte with all eight ones represents the biggest number you can count to with eight binary digits. If you try looking at all those ones, they blend together and it’s hard to tell exactly how many there are. But in hexadecimal, this would be the number 0xff. It’s shorter, simpler, and less confusing. That is once you get over the idea of using letters for numbers.