> urticator.net

  About This Site
> Domains

> Numbers

  Powers and Fractions
  Notes About Squares
  Decimal Expansions
  Repeat Length Again
  Number Maze
  Intrinsic Nature of Primes
> Other Topics
  Other Topics (2)

> Hexadecimal
  Base 60
  Negative Digits
  Two Kinds of Odd


Hexadecimal is a wonderful thing. I became familiar with it early on, and have often been glad I knew it. Sure, it's useful mainly for working with computers, but the way I see it, learning a different number base is mind-expanding in much the same way that learning a foreign language is.

Hexadecimal for me occupies about the same place that German does. I know them both pretty well, enough to be comfortable with them, but I never learned to speak them like a native or to think in terms of them. Neither was a large step away from my native language. And, coincidentally, I learned them both at about the same time.

Hexadecimal is also nice because it's in the same language family as binary and octal (all descended from proto-binary, hehe). Once you know one, it's easy to translate to any of the others.

Although the factor structure is very different, 24 vs. 2Χ5, hexadecimal doesn't strike me as very foreign, I think because the base is still an even number. I bet if one really learned to think in foreign bases, it would be amazing. I imagine one would be able to see the numbers and their relationships almost as Platonic ideals, free of any accident of base.

But, it's hard to know where to begin, what number to use as a base. Maybe 13, an odd prime? Or 15, the first odd composite, or 21, an odd composite relatively prime to 10? Or maybe 12 or 24, composite numbers with repeated factors, or 30 or 60, numbers with three different prime factors? Unfortunately, I can't really imagine learning a base beyond 60, there would just be too many symbols.

Now I'm sure you're all inspired to learn hexadecimal. Fortunately, it's easy, pretty much everything I know fits in one little table.


The first thing you need to do is learn the part in bold. The other digits are the same as in decimal, so that's all you need to know to convert single digits from hex to decimal.

Then you need to learn the fourth column, which gives the decimal values for hex digits in the tens place. For example, hexadecimal 90 is decimal 144. If you know that, and can add in your head a little, you can easily convert two-digits numbers (bytes!) from hex to decimal.

If you're enthusiastic, you could learn the fifth column, and even extend the table further, but I've never found it necessary. For example, even though it's an easy one, I just don't know that A00 is 2560 in the same reflexive way I know that A0 is 160. I only included the column because it's striking how many of the values are powers of two and three (sometimes with a zero tacked on).

To convert from decimal to hex, I always just reverse the method above, using a kind of table lookup. For decimal 107, for example, I think, hmm … the next number down is … hmm … 96, which is 60; and 107 - 96 leaves 11, which is B; so the answer is 6B. It would be much more efficient to learn the reverse digit tables, but I've never bothered. I know that 10, 20, and 30 are A, 14, and 1E, and that 100, by amusing coincidence, is 64, but that's all.

So much for conversion.

It's not quite true what I said above, that I never learned to think in terms of hexadecimal. There are a few things that I think of mostly in hex.

  1. Bitwise operations. If for some reason I need to do a bitwise operation (AND, OR, XOR, NOT) on actual numbers, instead of just abstractly in code, I'll almost certainly convert the numbers to hex, or, rather, leave them in hex, because why would they be in decimal in the first place? Then, for the actual operation, I'll convert them to binary and back, digit by digit.

    Actually, in hex the NOT operation is the same as complement I; I know how to do that without converting to binary.

  2. Masks. Having two actual numbers for a bitwise operation is rare, but having just one is pretty common, and in that case the one number is known as a mask. I know masks slightly better in hex than in decimal. If, for example, I was writing a base64 encoder, and wanted to extract the six low bits of a number x, I'd write (x & 0x3F).
  3. Shift operations. How I implement a shift depends on the situation. If I'm already working in decimal, and only need to shift a few places left or right, I'll probably just stay in decimal and apply the appropriate shift operation repeatedly. If I'm in hex, and need to shift by a multiple of four, well, that's so easy I don't even realize I'm doing anything. If I'm off by one, I might try to do the extra shift in hex; otherwise, as with the bitwise operations, I'll probably convert to binary and back.
  4. ASCII. I don't quite know all the ASCII codes, but I do know a lot of them, and I remember them all in hexadecimal. It's very convenient that way, because the uppercase and lowercase letters fall into a nice pattern. E.g., “A” and “a” are 41 and 61, where in decimal they're 65 and 97.

    I first learned the codes on the Apple II, which didn't have lowercase, and which for some reason had the high bit set, so that “A” was C1. That seems very strange to me now! Somewhere along the line I stopped thinking of those codes, but now, possibly as a result of that experience, I don't remember “A” as either 41 or 61, I remember it as 1 (or 01).

    What's funny about that is, I never memorized the standard cryptographic numbers … you know, the ones where “A” is 1 and “Z” is 26. I know them up to about “J”, but after that, the most efficient way I have of finding them is to remember the ASCII code and convert to decimal!

  5. Opcodes. Speaking of the Apple II, I still know a lot of the opcodes for the 6502. I'll spare you the details, but as with ASCII, there are some nice patterns that are obvious in hexadecimal but not in decimal.

So, it's not quite true that I never learned to think in hex. But, there are large gaps in my education. I know the most basic thing a native would know, how to count, but not the next most basic thing, arithmetic. I can sort of fake addition and subtraction, but even there, there's a lot of back-and-forth to decimal; and I can only do multiplication and division in special cases. It's like knowing the vocabulary of a language, but not the grammar—and certainly not anything about writing good prose.

For your amusement, here's an example of something a native would know, a table of squares.


Doesn't it just jump right out at you that 9 + 10 = 19?


  See Also

  Base 60
  Expansion Calculator
  In Other Bases
  Multiplication Table, The
  Other Bases
  Outline, An
  Powers of 2 and 3
  Two Kinds of Odd

@ March (2004)