"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.

- The first column is 3 columns away from the rightmost column. 10 to the power of 3 is 10 times 10
times 10, which is 1000. The digit in this column is 1. 1000 times 1 is 1000. The number represented
by this column is 1000.
- The second column is 2 columns away from the rightmost column. 10 to the power of 2 is 10 times 10,
which is 100. The digit in this column is 9. 100 times 9 is 900. The number represented by this column
is 900.
- The third column is 1 column away from the rightmost column. 10 to the power of 1 is 10. The digit is
this column is 0. 10 times 0 is 0. The number represented by this column is 0.
- The fourth column is 0 columns away from the rightmost column -- it is the rightmost column. 10 to the power of 0 is 1. The digit in this column is 6. 1 times 6 is 6. The number represented by this column is 6.

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.

- The first column is 4 columns away from the rightmost column. 2 to the power of 4 is 2 times 2 times 2
times 2, which is 16. The digit in this column -- and binary digits are called "bits", so that's what we'll
call them from here on out -- is 1. 16 times 1 is 16. The number represented by this column is decimal
16.
- The next column is 3 columns away from the rightmost column. 2 to the power of 3 is 2 times 2 times
2, which is 8. The bit in this column is 0. 8 times 0 is 0. The number represented by this column is
decimal 0.
- The next column is 2 columns away from the rightmost column. 2 to the power of 2 is 2 times 2, which
is 4. The bit in this column is 1. 4 times 1 is 4. The number represented by this column is decimal 4.
- The next column is 1 column away from the rightmost column. 2 to the power of 1 is 2. The bit in this
column is 0. 2 times 0 is 0. The number represented by this column is 0.
- The final column is 0 columns away from the rightmost column -- it is the rightmost column. 2 to the power of 0 is 1. The bitin this column is 1. 1 times 1 is 1. The number represented by this column is decimal 1.

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 times 1 is 32768
- 16384 times 0 is 0
- 8192 times 0 is 0
- 4096 times 1 is 4096
- 2048 times 1 is 2048
- 1024 times 1 is 1024
- 512 times 0 is 0
- 256 times 1 is 256
- 128 times 0 is 0
- 64 times 0 is 0
- 32 times 1 is 32
- 16 times 1 is 16
- 8 times 0 is 0
- 4 times 1 is 4
- 2 times 0 is 0
- 1 times 1 is 1

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

Now, the hex.

- First column: 16 to the 3rd is 4096. The digit in this column is 9. 4096 times 9 is 36864.
- Second column: 16 to the 2nd is 256. The digit in this column is D (decimal 13). 256 times 13 is 3328.
- Third column: 16 to the 1st is 16. The digit in this column is 3. 16 times 3 is 48.
- Fourth column: 16 to the 0th is 1. The digit in this column is 5. 1 times 5 is 5.

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

- Meanings of the columns in a five-digit decimal number: 10000s, 1000s, 100s, 10s, 1s (as in, how
many 10000s there are plus how many 1000s there are plus etc. s is not a variable here, just a
pluralizer.)
- Meanings of the columns in a sixteen-bit binary number: 32768s, 16384s, 8192s, 4096s, 2048s, 1024s,
512s, 256s, 128s, 64s, 32s, 16s, 8s, 4s, 2s, 1s
- Meanings of the columns in a four-digit hex number: 4096s, 256s, 16s, 1s

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:

- 4096s times (8s, 4s, 2s, 1s) = 32768s, 16384s, 8192s, 4096s
- 256s times (8s, 4s, 2s, 1s) = 2048s, 1024s, 512s, 256s
- 16s times (8s, 4s, 2s, 1s) = 128s, 64s, 32s, 16s
- 1s times (8s, 4s, 2s, 1s) = 8s, 4s, 2s, 1s

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