In the previous block, we broke down our every day base10 system into columns, multiplied by a power of 10, then added together. With this method, we can build systems using any number for a base by just changing the powers. For example, let's examine a ternary (base3) numbering system. In a base3 system, each column represents a power of 3 (rather than 10, as in the decimal system). Also, the system has a radix of 3, meaning each column can take on only 3 different values: 0, 1, or 2.
ex. Write the base3 number
in the decimal (base10) system:
(the right-hand-side of the equations are all in base10)
So the number
, a number written using
as a base, is the
in the base10 system.
Converting a non-decimal system to base10 is straight-forward:
In the base10 system (where we're used to doing our math), multiply each digit from the number in the alternate base (base3 in the above example) by the power of the base that's appropriate for that column.
Add the products together (still in base10). The result is the number that was originally written in an alternate base, now written in base10.
The exact same process for converting to base10 can be used to convert from base10 to another base. However, all of the math on the right-hand-side ( multiplication and addition) would need to be done in the alternate base system for it to work. So while it's possible... our brains are used to doing math in base10, so usually use subtraction instead. Here's an abstract step-by-step (we'll take a look at an example right after so it makes more sense):
Find the highest
multiple * powerof the base that will fit into the base10 number.
Write the alternate base number in the correct column, leaving blanks to the right of it (to fill in later steps).
Subtract the amount equivalent to the alternate base number from your current base10 number. This is now your remainder.
Jump back to step 1, using this remainder instead of your original number.
Repeat steps 1-4 until your remainder is 0.
ex. Write the decimal number
in a base4 system.
Find the highest
mulitple * power
that will fit in
. In base10,
, which is too high. So we'll start
for our multiple- so we get
because it's a base4 system, the radix is 4, meaning that each column
can only take on 4 possible values: 0, 1, 2, 3.
We'll place a
column, which is the 4th one to the left.
The remaining places we'll leave blank
Now we can subtract
from our original
, since we're accounting
in the 4th column of our new base4 number.
Using our remainder of
, jump back to step 1 and continue the
process. The next column is
, and the highest multiplier of
that will fit into
we'll put a
Subtract our newest multiplier from our working remainder:
So our new remainder is 11. Repeating the process again, our next column's
power of 4 is
. The highest multiplier is 2 (
Calculate our new remainder:
With the new remainder of 3, and the final column being essentially a 1's column,
we can fill in the final column with just
written in base4 is
It might seem like a lot of work, but luckily, most of a programmer's work in other number systems involves only a handful of other bases. The more you work in these other base systems, the easier it is to convert back and forth. The two most common systems that you'll see while programming are binary, a base2 system, and hexadecimal, a base16 system. Other common systems are base8 (octal) and base64.