CS 273: Radix Conversions

Interpreting Decimal Numbers

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:

  1. Multiply the value we've already got by 10 (giving us 20), and
  2. Add the second digit.

Once we've done that, we've gotten a value of 25, with one digit left to interpret.

To interpret the third digit, we:

  1. Multiply the value we've already got by 10 (giving us 250), and
  2. Add the third digit.

So: we've interpreted the digits of the number, and retrieved its value.

We can organize our thoughts with the help of a table:

OldDigitNew
0 2  
  5  
  3  

We fill in this table using the algorithm we just described. When we're done, it looks like this:

OldDigitNew
0 2 2
2 5 25
25 3 253

Converting From Another Radix to Decimal

The way we convert from some other radix to decimal is, once again, to use Horner's rule. Once again, we take each digit in turn, working from left to right, and add it in to the value we're accumulating. If we take the number 100101 2 , we are evaluating With a little rearranging, this becomes So we take 0, multiply by 2, add 1, multiply the total by 2, add 0, and so forth. We can write this up as a table. First, we set up a table like this, using the bits in the number for the contents of the ``Digit'' column and a 0 as the first entry in the ``Old'' column:

OldDigitNew
0 1  
  0  
  0  
  1  
  0  
  1  

The way we fill in the table is we do the following on each row:

  1. Add the Old column to the Digit column and put the result in the New column
  2. If we're not on the last row, multiply the contents of the New column by the radix you're converting from (2 in this case) and put the result in the Old column of the next row.
So when we've filled the table in, it looks like this:

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.

Converting From Decimal to Another Radix

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:

OldNewDigit
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:

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

The Answer to Life, the Universe, and Everything

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


Last modified: Fri Aug 28 09:36:58 MDT 2009

Valid HTML 4.01 Transitional