Concept

Why is hexadecimal used in computer science, and what do its digits represent?

Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk / Chapter 1

"Now objectively speaking, it turns out that ten is a pretty weird base too. I know it doesn’t seem like it, but that’s only because we’re so used to it. Really, if you’re repeatedly adding little circles to a drawing, ten is a funny place to decide to draw the line and go to more digits. It’s only divisible by 2 and 5 (of all things), it’s not a perfect square, and all this makes it kind of an awkward choice. In computer science, it turns out to be very (very) convenient to use a base that is a power of two. This means a base that is “two-to-the-something.” In earlier computing days, octal (base 8) was a common choice. But for various reasons, that turns out to be less convenient than using base 16, or hexadecimal. Any time you’re working with hardware, operating systems, device drivers, bit masks, or anything else low level, you’ll encounter numbers written in base 16 a heck of a lot. So let’s study this particular base in some detail. Base 16 will need sixteen digits, of course. Unfortunately, we ten-fingered people have only invented ten symbols that are obviously numerical: the digits 0 through 9. So what do we do for the other six? It turns out that the originators of this system took perhaps the most obvious approach: repurposing the letters of the alphabet. So we add the “digits” A through F (sometimes written as capitals, sometimes in lower-case) to our set of symbols. These, then, are the quantities that each individual digit represents: 0 zero, 1 one, 2 two, 3 three, 4 four, 5 five, 6 six, 7 seven, 8 eight, 9 nine, A ten, B eleven, C twelve, D thirteen, E fourteen, F fifteen. The inventors of hexadecimal notation didn’t have to use the alphabet, of course; they could have chosen a star for ten, a square for eleven, a happy face for twelve, etc., but that wouldn’t have been very easy to type. So we’re stuck with the letters, for better or for worse. Practice staring at that letter A and saying the word “ten.” Because that’s what it means. In hexadecimal, the sequence of digits 10 does not mean “ten.” It means “sixteen.” Those are the symbols. What are the place values? Well, they are (from the right) the 16^0’s place, the 16^1’s place, the 16^2’s place, and so on. Written decimally, those work out to be the 1’s place, the 16’s place, the 256’s place, the 4096’s place, and so on. Again, those numbers seem strange only because when they are written decimally they don’t come out very “round.” The value of a number like 72E3 is computed as: 72E3_16 = 7 × 4096_10 + 2 × 256_10 + 14 × 16_10 + 3 × 1_10 = 29,411_10. Notice we treated the “E” just like another digit, which it is. We also called 72E3 “a number,” which it is. Get used to the idea that numbers — totally legitimate numbers — can have letters for some of their digits. In hexadecimal, what’s the highest value that can fit in one digit? Answer: F (which is fifteen.) What’s the highest that can fit in two digits? FF (which is two hundred fifty-five.) What about three digits? FFF (which is sixty-five thousand five hundred thirty-five.) And so on. If you count in hexadecimal, you do the same thing as in decimal, only you “roll over the odometer” when you get to F, not when you get to 9."

Related Ideas

Why is hexadecimal used in computer science, and what do its digits represent? | Stephen Davies, Ph.D. Version 2.2.2 Through Discrete Mathematics A Cool Brisk Walk | Bifalgorithm | Bifalgorithm