In order to see where we're going with the radix conversion
algorithm, we'll start by seeing how we can interpret a decimal
number using Horner's rule for evaluating a polynomial. Here's our
example number: 253. Let's consider this number
one digit at a time.
The first digit is a 2. When we've interpreted the
first digit, we have a value of 2, and two digits left to
interpret.
To interpret the second digit, we:
Once we've done that, we've gotten a value of 25, with one digit left to interpret.
To interpret the third digit, we:
So: we've interpreted the digits of the number, and retrieved its value.
We can organize our thoughts with the help of a table:
| Old | Digit | New |
|---|---|---|
| 0 | 2 | |
| 5 | ||
| 3 |
We fill in this table using the algorithm we just described. When we're done, it looks like this:
| Old | Digit | New |
|---|---|---|
| 0 | 2 | 2 |
| 2 | 5 | 25 |
| 25 | 3 | 253 |
| Old | Digit | New |
|---|---|---|
| 0 | 1 | |
| 0 | ||
| 0 | ||
| 1 | ||
| 0 | ||
| 1 |
The way we fill in the table is we do the following on each row:
| Old | Digit | New |
|---|---|---|
| 0 | 1 | 1 |
| 2 | 0 | 2 |
| 4 | 0 | 4 |
| 8 | 1 | 9 |
| 18 | 0 | 18 |
| 36 | 1 | 37 |
The decimal result is in the bottom row of the ``New'' column.
I actually don't make any claim that this is easier for people to do than just looking at the positions and multiplying by the right powers. But it's a whole lot easier to put in a computer program!
This is called the multiplication method, as we multiply in each step.
We start by looking at what happens when we divide a decimal number by 10. For an example, we'll use 12,573. If we divide it by 10 we get 1,257, with a remainder of 3. The remainder of the division is the least significant digit of the original number. If we divide 1,257 by 10, we get 125 with a remainder of 7 This gave us the next digit. If we keep dividing by the radix, each remainder in turn is another digit.
Let's take our 37 10 and see what happens when we do a bunch of divisions by 2. A convenient way to show this, like with the multiplication methond, is with a table, where we have one column for the current number we're working with, another column for the result of dividing that number by the radix, and a third column for the digits we obtain. When we start, we'll just have the number we want to convert in the upper left corner, like this:
| Old | New | Digit |
|---|---|---|
| 37 | ||
We fill in the table row by row: we divide the number in the
Old column by the new radix (2 in this case), and put
the quotient in the New column and the remainder in
the Digit column. If the quotient is non-zero, we start
a new row, copying from the New column of the old row
to the Old column of the new row. When we're done,
the table looks like this:
| Old | New | Digit |
|---|---|---|
| 37 | 18 | 1 |
| 18 | 9 | 0 |
| 9 | 4 | 1 |
| 4 | 2 | 0 |
| 2 | 1 | 0 |
| 1 | 0 | 1 |
Since we obtained the results starting from the least significant digit, we read it starting at the bottom of the rightmost column of the table and working up, and the result is 1001012
This algorithm for converting a number from decimal to binary is called the division algorithm, because we divide on every step. In general, we can convert from decimal to any radix we want using this algorithm, dividing by our intended radix on every step.
Readers of The Hitch Hiker's Guide To The Galaxy have encountered the Answer to Life, the Universe, and Everything: after much computer effort, it was revealed that the answer was 42. It seems they didn't realize the Answer might not make much sense if they didn't have a clear understanding of the Question...
Not many readers remember that, in one of the later books of the series (I think the second), the Question is "how much is 6 times 9?"
There is actually a radix in which this is correct... (though Adams said it was a coincidence — he didn't do this deliberately).