Dad’s nerd

My dad was thirty when I was born, so a few years ago I had the opportunity to send him this for his birthday. Happy Father’s Day in heaven, Dad!

A birthday greeting

(Stick with it, it’s worth it.)

The counting system we normally use is called “base ten.”  It uses ten distinct digits (zero through nine), and the columns of a multi-digit number are powers of ten.  The rightmost column is “ones” (1 is 10 to the power of 0), the next column is “tens” (10 to the power of 1), the next column is “hundreds” (10 to the power of 2), and so on.

Example: the number 412 is understood as “four hundreds plus one ten plus two ones.”

Base ten is convenient for humans, who have ten fingers to count on.  But it’s inconvenient for computers, which count using “bits,” each of which is a microscopic on-off switch.  This makes it more convenient for computers to count in “base two,” which uses two distinct digits (zero and one, corresponding to a bit that’s off or on).  The columns of a multi-digit base-two number are powers of two.  The rightmost column is still “ones” (because 2 to the power of 0 is 1) but the next column is “twos,” then “fours,” then “eights,” then “sixteens,” and so on.

Example: the base-two number 110011100 is understood as “one 256 plus one 128 plus one 16 plus one 8 plus one 4” (which is the same as the base-10 number 412, by the way).

As you can see, base two requires many more columns than base ten to express the same number.  Computers don’t care about that – but the people who work with computers do.  Base two counting is very inconvenient for them.  But converting back and forth between base 10 and base 2 is onerous, because even computer nerds don’t like doing unnecessary arithmetic.

One common solution is to deal not in base two or in base ten but in base eight.  Base eight uses eight distinct digits (zero through seven) and the columns of a base-eight number are “ones,” “eights,” “sixty-fours,” and so on.

You might think that adding yet another counting system is a needless complication, but the cool thing about base eight is that each group of three base-two digits always corresponds to the same base-eight digit:

Base twoBase eight
0000
0011
0102
0113
1004
1015
1106
1117

This makes converting between base two and base eight easy.  Just take three base-two digits at a time (from right to left) and replace them with their base-eight counterpart:

110011100 -> 110 011 100 -> 634 (which is the base-eight version of 412).

Base eight is often called “octal,” and base ten is often called “decimal.”  Now you have enough information to understand this old programmer’s joke:

Q: Why is Halloween like Christmas?
A: Because OCT 31 = DEC 25!

But we’re not done.  Base eight is handy, but not as handy as it can be, because as you probably know, computer memory is divided into bytes, and each byte is eight bits long.  You can’t evenly divide eight bits into groups of three.  And there’s some ambiguity when you string together multiple bytes in a row.  Suppose you’re dealing with these two bytes:

11010001  10011100

Should you group these sixteen bits into threes like this:

11 010 001  10 011 100 -> 321234 (base eight)

(where each byte is grouped into threes separately) or like this:

1 101 000 110 011 100 -> 150634 (base eight)

(where all sixteen bits are strung together and then separated into groups of three)?

To avoid these problems, it’s more common for computer folks to use not base two, not base ten, and not base eight, but base sixteen!  Base sixteen has sixteen distinct digits: zero through nine as in base ten, then A for a single-digit “ten,” B for a single-digit “eleven,” C for a single-digit “twelve,” D for “thirteen,” E for “fourteen,” and F for “fifteen.”  The columns of a base-sixteen number are “ones,” “sixteens,” “two-hundred-fifty-sixes,” and so on.

In base sixteen, 412 is written as 19C.  (One 256 plus 9 sixteens plus twelve.)

Like base eight, base sixteen – which is also called “hexadecimal” (or “hex” for short) – is easily converted to and from base two, because every group of four base-two digits corresponds to a base sixteen digit.

Base twoBase sixteen
00000
00011
00102
00113
01004
01015
01106
01117
10008
10019
1010A
1011B
1100C
1101D
1110E
1111F

And grouping base-two digits four at a time instead of three at a time makes it very natural to represent any eight-bit byte as a two-digit hexadecimal number.

11010001  10011100 -> D1 9C

Now for the punchline of this nerdy shaggy-dog story.  This year you are turning 80 and I am turning 50.  And DEC 80 = HEX 50!

This is the kind of thing that makes nerds like me turn cartwheels of joy.  And before you roll your eyes or shake your head, let me just remind you of who raised this particular nerd.

Happy birthday, Dad.  I love this numeric coincidence, but not as much as I love you.