(footnote on binary and hex)

"Back to basics" ideologues like to take potshots at "The New Math" from education's hippie days. New Math's emphasis on alternate bases is a favorite target: New Math made a point of teaching kids that base-10 is just an arbitrary convention and that numbers can be expressed just as well with columns representing powers of some other number, like 2. So is this a faintly subversive waste of time, as the critics contend? Well, no. Not only does teaching alternate bases lead to a deeper understanding of arithmetic in general, it also comes in quite handy when dealing with computers. For computer math is not decimal, but binary.

Base 10 math uses ten different digits, 0 through 9. But modern computers don't equate ten different voltage levels with different digits: either there's high voltage running through a certain point or low voltage. Two digits to choose from. 1 or 0. This makes base-2 the order of the day. So, in case you've never encountered or have forgotten alternate-base math, here's a quick overview.

First, exponents (or powers). The phrase "to the power of" means "multiply the following number of this number together". So, "three to the power of four" (or "three to the fourth power") means to take four 3s and multiply them together: 3 times 3 times 3 times 3. A number to the power of one is just itself. A number to the power of zero is always 1.

Every number has a certain number of columns. Each column represents a number that is added to the other numbers represented by the other columns, and the total is the quantity represented by the whole number. The number represented by any given column is the base being used to the power of how many columns away the column is from the rightmost column, times the digit in the column. This is very cumbersome to express abstractly, but it's easy in practice. Here are some examples.

We'll start with base 10. Let's take as an example the number 1906.

Now we add the columns together. 1000 + 900 + 0 + 6 = 1906. This is what we started with, as is to be expected when converting decimal (base 10) to decimal. But now that we've seen a trivial example, let's change bases and convert base 2, which a computer uses, to base 10, which is what most humans think in.

Let's take a binary number (that is, a number in base 2). We'll pick 10101.

Add the columns together. 16 + 0 + 4 + 0 + 1 = decimal 21. Binary 10101 = decimal 21.

Binary numbers get very long very quickly. Earlier we used decimal 1906 as an example; in binary, this is 11101110010. Thus, most programmers choose to use a more efficient base. Base 10? No, it's much easier to use a power of 2, which is why hexadecimal, base 16, is the usual solution. Hexadecimal (often called "hex") uses the digits 0 through 9 just like decimal, and then uses A through F to represent decimal 10 through decimal 15. It's convenient for programmers because binary numbers can be converted to hex numbers four bits at a time. For instance, let's take a long binary number, like 1001110100110101. To convert this into hex, we just break it up into four-bit units:

   1001 1101 0011 0101

and turn each of those units into a hex digit. 1001 = 9, 1101 = D (decimal 13), 0011 = 3, 0101 = 5. So the hex version of that long binary number is 9D35. Now, to check that it really is as easy as that, let's make sure they equal the same thing in decimal (compressing the process a bit this time):

32768 + 0 + 0 + 4096 + 2048 + 1024 + 0 + 256 + 0 + 0 + 32 + 16 + 0 + 4 + 0 + 1 = decimal 40245.

Now, the hex.

36864 + 3328 + 48 + 5 = 40245. Ta-da! So it worked. Why does it work? Look at it this way:

Now, take a hex digit and convert it into its binary equivalent: hex 1s = binary (8s, 4s, 2s, 1s). Now multiply that through each of the hex columns:

As you can see, these are exactly the same as the binary columns. But the only column shared by binary/hex numbers and decimal numbers is the 1s column, so decimal is not a convenient base where computers are concerned.


Return to from source code to virtual machine code
Or return to the table of contents