Skip to main content

Working with arbitrary bases

Daniel Farlow
Software Engineer

This post details how to work with arbitrary bases in a positional numeral system (e.g., decimal, binary, etc.). Specifically, this post details how to convert numbers from base 10 to base bb in such a way that not only is the whole number portion converted but also the fractional/radix portion as well.

Attribution

Much of the discussion that follows has been taken from [12].

Division Algorithm

The division algorithm, as stated in [14], is as follows:

Let aa be an integer and dd a positive integer. Then there are unique integers qq and rr, with 0r<d0\leq r < d, such that a=dq+ra=dq+r.

In the equality given above, dd is called the divisor, aa is called the dividend, qq is called the quotient, and rr is called the remainder.

Arbitrary bases (description)

We recall that to represent a number in a positional numeral system with base bb we need basic symbols for the integers zero up through b1b - 1. Even though the base b=10b = 10 is such an important part of our culture, the choice of 10 is really quite arbitrary, and other bases have great practical and theoretical importance. If b10b\leq 10, we may use our ordinary digit symbols; thus, for example, we may consider 3012 as a number expressed to base 4 with the basic symbols 0, 1, 2, 3. To make clear that the number is considered as expressed to base 4, we shall write it as (3012)4(3012)_4. When no subscript is written, it will be understood in this treatment that the number is expressed to the ordinary base 10. If b>10b > 10, we must augment our digit symbols by some new basic symbols, for we always need bb basic symbols. If b=12b = 12, therefore, we may take 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, tt, ee for our basic symbols, where tt and ee are symbols for ten and eleven; for example, we might have (3t1e)12(3t1e)_{12}.

It is easy to convert a number from a given base to the ordinary base 10. Thus, we have

(3012)4=3(43)+0(42)+1(4)+2=198(3012)_4=3(4^3)+0(4^2)+1(4)+2 = 198

and

(3t1e)12=3(123)+10(122)+1(12)+11=6647.(3t1e)_{12}=3(12^3)+10(12^2)+1(12)+11=6647.

If we have a number expressed in the ordinary scale, then we may express it to base bb as follows. Letting NN be the number, we have to determine the integers an,an1,,a0a_n, a_{n-1},\ldots, a_0 in the expression

N=anbn+an1bn1++a2b2+a1b+a0,N=a_nb^n + a_{n-1}b^{n-1}+\cdots+a_2b^2+a_1b+a_0,

where 0ai<b0\leq a_i < b. Dividing the above equation by bb, we have

Nb=anbn1+an1bn2++a2b+a1+a0b=N+a0b.\frac{N}{b}=a_nb^{n-1}+a_{n-1}b^{n-2}+\cdots+a_2b+a_1+\frac{a_0}{b}=N' + \frac{a_0}{b}.

That is, the remainder a0a_0 of this division is the last digit in the desired representation. Dividing NN' by bb, we obtain

Nb=anbn2+an1bn3++a2+a1b,\frac{N'}{b} = a_nb^{n-2}+a_{n-1}b^{n-3}+\cdots+a_2+\frac{a_1}{b},

and the remainder of this division is the next to the last digit in the desired representation. Proceeding in this way, we obtain all the digits a0,a1,,ana_0,a_1,\ldots,a_n. This procedure can be systematized quite conveniently, as shown below. Suppose, for example, we wish to express 198 to the base 4. We find

The desired representation is (3012)4(3012)_4. Again, suppose we wish to express 6647 to the base 12, where tt and ee are employed to represent ten and eleven, respectively. We find

The desired representation is (3t1e)12(3t1e)_{12}.

One is apt to forget, when adding or multiplying numbers in our ordinary system, that the actual work is accomplished mentally and that the number symbols are used merely to retain a record of the mental results. Our success and efficiency in carrying out such arithmetic operations depend on how well we know the addition and multiplication tables, the learning of which absorbed so much of our time in the primary grades. With corresponding tables constructed for a given base bb, we can similarly perform additions and multiplications within the new system, without spending any time reverting to the ordinary system.

Let us illustrate with base 4. We first construct the following addition and multiplication tables for base 4.

The addition of 2 and 3, therefore, by reference to the table, is 11, and the multiplication of 2 and 3 is 12. Using these tables, exactly as we are accustomed to using the corresponding tables for base 10, we can now perform additions and multiplications. As an example, for the multiplication of (3012)4(3012)_4 by (233)4(233)_4 we have, omitting the subscript 4,

Considerable familiarity with the tables will be needed in order to perform the inverse operations of subtraction and division. This, of course, is also true for the base 10 and is the reason for much of the difficulty encountered in teaching the inverse operations in the elementary grades.

Radix fractions (description)

Before attempting problems in the next section concerning radix fractions, it may help to make some general number representation explorations first. We see in [12] that any whole number NN can be written uniquely in the form

N=anbn+an1bn1++a2b2+a1b+a0,N=a_nb^n + a_{n-1}b^{n-1}+\cdots+a_2b^2+a_1b+a_0,

where 0ai<b0\leq a_i < b. We can roughly and informally represent any number in base bb as follows:

N=+ab3+cb2+db1+eb0+fb1+gb2+N=\cdots+ab^3+cb^2+db^1+eb^0+fb^{-1}+gb^{-2}+\cdots

Such a number would notationally be represented as N=acde.fgN=\cdots acde.fg\cdots (base bb). It is worth noting that the fraction may go on indefinitely (as is the case with 13=3\frac{1}{3}=\overline{3}), and the little dot . between acde\cdots acde and fgfg\cdots is referred to as the "radix point" or simply the "decimal point" when working with conventional base 10 numbers. Converting the number 12.112 (base 3) to base 10 is fairly simple if we just follow the informal representation given above:

(12.112)3=1(31)+2(30)+1(31)+1(32)+2(33)=3+2+13+19+227=14927=5.518518(12.112)_3 =1(3^1)+2(3^0)+1(3^{-1})+1(3^{-2})+2(3^{-3})=3+2+\frac{1}{3}+\frac{1}{9}+\frac{2}{27}=\frac{149}{27}=5.518\overline{518}

More formally, suppose we want to represent a general number NN in base bb that accounts for both its whole number and fractional portions. We could write NN as

N=i=αβaibi0ai<b;α<0β;a,b,i,α,βZN=\sum_{i=\alpha}^{\beta} a_ib^i\qquad 0\leq a_i < b;\quad \alpha < 0 \leq\beta;\quad a,b,i,\alpha,\beta\in\Z

where the idea is that β\beta and α\alpha effectively serve as upper and lower bounds, respectively, with β\beta serving as the highest power of base bb needed to represent the number in question and the magnitude of α\alpha serving as how many digits to the right of the radix point we want to record (e.g., the hundredth place for a decimal fraction would be represented by α=2α=2\alpha=-2\to |\alpha|=2 places to the right of the decimal point). Hence, the whole number portion of NN could be represented by i=0βaibi\sum_{i=0}^\beta a_ib^i while its fractional portion could be represented by i=α1aibi\sum_{i=\alpha}^{-1}a_ib^i.

All of this is a bit theoretical. What would this look like in practice? We can use our work above in converting (12.112)3(12.112)_3 to base 10 as an example. Since the highest power of 3 needed to represent this number is 1, we have β=1\beta=1. Since we have 3 digits to the right of the radix point, we have α=3\alpha=-3. Our generalized notation gives us the following:

(12.112)3=i=31aibi(since α=3 and β=1)=a1b1+a0b0+a1b1+a2b2+a3b3(by definition of Σ)=1(31)+2(30)+1(31)+1(32)+2(33)(by previous work and since b=3)=14927(simplify)=5.518518(simplify)\begin{align*} (12.112)_3 &= \sum_{i=-3}^1 a_ib^i & (\text{since $\alpha=-3$ and $\beta=1$})\\[1em] &= a_1b^1+a_0b^0+a_{-1}b^{-1}+a_{-2}b^{-2}+a_{-3}b^{-3} & (\text{by definition of $\Sigma$})\\[1em] &= 1(3^1) + 2(3^0) + 1(3^{-1}) + 1(3^{-2}) + 2(3^{-3}) & (\text{by previous work and since $b=3$})\\[1em] &= \frac{149}{27} & (\text{simplify})\\[1em] &= 5.518\overline{518} & (\text{simplify}) \end{align*}

Converting a number from base 10 to another base is somewhat trickier. The method for converting a base 10 number to another base is conveniently described in the previous section, but this method only works for those numbers to the left of the decimal point (whole numbers). We need to deal with the numbers to the right of the decimal point (the "fraction part"). To do this, we use a different and sort of backward version of the original method described in the previous section. The original method starts by dividing the number NN by the base bb, and we then use the remainders to form the desired number in base bb. We arrange the new number NN by putting together the remainders from bottom to top. This works for whole numbers.

To convert the fractional part of a number to another base, we must essentially use the method above in "reverse." We take the fractional part of the number (the portion to the right of the decimal or radix point) and multiply it by bb. The integer parts that result from these multiplications play the same role that the remainders played when dividing by base bb in order to convert a whole number to a different base. We proceed to multiply whatever fractional part is left and we produce the number NN in the specified base bb in this way.

For example, to convert the number 14.73 to base 3, we must do the following: we first start out with the portion of the number to the left of the radix point (the whole number portion):

Assembling the remainders from bottom to top, we get 112 in base 3. That takes care of the whole number portion of 14.73. We must now convert .73 to base 3 to finish the complete conversion. As described above, the fractional conversion is basically the reverse of the previous method in the sense that now we will be multiplying by base bb, keeping the resultant integer parts, and then assembling these parts from top to bottom instead of bottom to top:

As seen above, we arrange the integer parts from top to bottom to get the fractional portion in base 3. Thus, we have the following (note that we could write down more fractional digits had we continued the process above):

14.73=(14.73)10=(112.201201)314.73 = (14.73)_{10} = (112.201201)_3

In a nutshell, the method used above is the one you will want to use for converting the fractional part for a number in base 10 into base bb. The following section provides a more formalized description of this process.

Expressing a number N in base b

If we have a number NN that is expressed using base 10, then we may express it using base bb if we can determine the integer values of

an,an1,an2,,a2,a1,a0,a1,a2,,a(m1),ama_n,a_{n-1},a_{n-2},\ldots,a_2,a_1,a_0,a_{-1},a_{-2},\ldots,a_{-(m-1)},a_{-m}

in the expression

N=\underbrace{a_nb^n+a_{n-1}b^{n-1}+a_{n-2}b^{n-2}+\cdots+a_2b^2+a_1b^1+a_0b^0}_{\text{integer portion}}+\underbrace{a_{-1}b^{-1}+\cdots+a_{-(m-1)}b^{-(m-1)}+a_{-m}b^{-m}+\cdots}_{\text{fractional portion}},}

where 0ai<b0\leq a_i < b and the integer or whole number portion of NN, as noted in braces above, is given by

anbn+an1bn1+an2bn2++a2b2+a1b1+a0b0,a_nb^n+a_{n-1}b^{n-1}+a_{n-2}b^{n-2}+\cdots+a_2b^2+a_1b^1+a_0b^0,

while the fractional portion of NN is given by

a1b1++a(m1)b(m1)+ambm+.a_{-1}b^{-1}+\cdots+a_{-(m-1)}b^{-(m-1)}+a_{-m}b^{-m}+\cdots.

One will usually specify how many digits mm after the radix point are desired, thus eliminating the need for the trailing ++\cdots in the representation of the fractional portion of NN.

To effectively express NN using base bb, we need to employ a method that first converts the whole number portion of NN into base bb and then subsequently converts the fractional portion of NN into base bb. We may then link together the results of these conversions to express the entire number NN in base bb. To start, let us write N=R+QN = R + Q, where RR represents the whole number portion of NN, and QQ represents the fractional portion of NN.

Converting the whole number portion of N into base b

First write out the expression for the whole number portion of NN (we omit b0b^0 for the sake of clarity since b0=1b^0=1, and we also express b1b^1 as simply bb):

R=anbn+an1bn1+an2bn2++a2b2+a1b+a0(0ai<b)R=a_nb^n+a_{n-1}b^{n-1}+a_{n-2}b^{n-2}+\cdots+a_2b^2+a_1b+a_0\qquad (0\leq a_i < b)

Dividing the equation above by bb gives us the following:

Rb=anbn1+an1bn2++a2b+a1+a0b=R+a0b\frac{R}{b}=a_nb^{n-1}+a_{n-1}b^{n-2}+\cdots+a_2b+a_1+\frac{a_0}{b}=R'+\frac{a_0}{b}

That is, the remainder a0a_0 of this division is the last digit in the desired representation.

How the remainder part works

Recall the division algorithm stated at the beginning of this section. We let aa represent the dividend (the number to be divided), dd the divisor (the number doing the dividing), and the claim was that unqiue integers qq and rr existed such that a=dq+ra=dq+r, where 0r<d0\leq r < d. We call qq the quotient and rr the remainder.

In our case, RR is the dividend (the whole number portion of NN to be divided), and bb is the divisor (the number doing the dividing). The following way of rewriting RR may more clearly illustrate why a0a_0 is the remainder when dividing by bb:

Rdividend=anbn+an1bn1+an2bn2++a2b2+a1b+a0(by definition)=b(anbn1+an1bn2++a2b+a1)+a0(factor out b strategically)=bdivisorRquotient+a0remainder(by definition of R)\begin{align*} \overbrace{R}^{\text{dividend}} &= a_nb^n+a_{n-1}b^{n-1}+a_{n-2}b^{n-2}+\cdots+a_2b^2+a_1b+a_0 & (\text{by definition})\\[1em] &= b(a_nb^{n-1}+a_{n-1}b^{n-2}+\cdots+a_2b+a_1) + a_0 & (\text{factor out $b$ strategically}) \\[1em] &= \underbrace{b}_{\text{divisor}}\cdot\underbrace{R'}_{\text{quotient}} + \underbrace{a_0}_{\text{remainder}} & (\text{by definition of $R'$}) \end{align*}

If we use RR' as the new dividend (and keep bb as the divisor), then we get the following:

Rdividend=anbn1+an1bn2++a2b+a1(by previous work)=b(anbn2+an1bn3++a2)+a1(factor out b strategically)=bdivisorRquotient+a1remainder(by definition of R)\begin{align*} \overbrace{R'}^{\text{dividend}} &= a_nb^{n-1}+a_{n-1}b^{n-2}+\cdots+a_2b+a_1 & (\text{by previous work})\\[1em] &= b(a_nb^{n-2}+a_{n-1}b^{n-3}+\cdots+a_2) + a_1 & (\text{factor out $b$ strategically}) \\[1em] &= \underbrace{b}_{\text{divisor}}\cdot\underbrace{R''}_{\text{quotient}} + \underbrace{a_1}_{\text{remainder}} & (\text{by definition of $R''$}) \end{align*}

We continue this process until we obtain a quotient that is less than our base; that is, using the language of the division algorithm, we continue this process until we obtain a dividend that is less than its divisor. At that point, the "final quotient" is simply 0 and the remainder is the last quotient we obtained that is less than our base.

This procedural flow is pictured in the figure where the integer part of 14.73 is being converted to base 3:

The upshot of all of this is that we can proceed in the way outlined above to obtain all the digits a0,a1,,ana_0, a_1, \ldots, a_n. The figure above simply provides a convenient systematization of the procedure.

We obtain all the digits a0,a1,,ana_0, a_1, \ldots, a_n by proceeding in this manner (elaborated on in the note above).

Converting the fractional portion of N into base b

First write out the expression for the fractional portion of NN:

Q=a1b1+a2b2++a(m1)b(m1)+ambm+(0ai<b)Q=a_{-1}b^{-1}+a_{-2}b^{-2}+\cdots+a_{-(m-1)}b^{-(m-1)}+a_{-m}b^{-m}+\cdots\qquad (0\leq a_i < b)

If we multiply the equation above by bb, then we get the following:

Qb=b(a1b1+a2b2++a(m1)b(m1)+ambm+)(by definition of Q)=a1b0+a2b1++a(m1)b(m1)+1+ambm+1+(multiply)=a1+a2b1++a(m1)b(m1)+1+ambm+1+(since b0=1)=a1+(a2b1++a(m1)b(m1)+1+ambm+1+)(rewrite)=a1integer+Qfractional(by definition of Q)\begin{align*} Q\cdot b &= b(a_{-1}b^{-1}+a_{-2}b^{-2}+\cdots+a_{-(m-1)}b^{-(m-1)}+a_{-m}b^{-m}+\cdots) & (\text{by definition of $Q$})\\[1em] &= a_{-1}b^0 + a_{-2}b^{-1} + \cdots + a_{-(m-1)}b^{-(m-1)+1}+a_{-m}b^{-m+1}+\cdots & (\text{multiply})\\[1em] &= a_{-1} + a_{-2}b^{-1} + \cdots + a_{-(m-1)}b^{-(m-1)+1}+a_{-m}b^{-m+1}+\cdots & (\text{since $b^0=1$})\\[1em] &= a_{-1} + (a_{-2}b^{-1} + \cdots + a_{-(m-1)}b^{-(m-1)+1}+a_{-m}b^{-m+1}+\cdots) & (\text{rewrite})\\[1em] &= \underbrace{a_{-1}}_{\text{integer}} + \underbrace{Q'}_{\text{fractional}} & (\text{by definition of $Q'$}) \end{align*}

Recall that 0ai<b0\leq a_i < b; that is, aia_i is an integer. The expression above, namely Qb=a1+QQb = a_{-1} + Q', shows that a1a_{-1} and QQ' represent the resultant integer and fractional portions of the multiplication of QQ by bb, respectively. Of course, we can continue the process outlined above to obtain more fractional digits:

Qb=b(a2b1+a3b2++a(m1)b(m1)+1+ambm+1+)(by definition of Q)=a2b0+a3b1+a(m1)b(m1)+2+ambm+2+(multiply)=a2+a3b1+a(m1)b(m1)+2+ambm+2+(since b0=1)=a2+(a3b1+a(m1)b(m1)+2+ambm+2+)(rewrite)=a2integer+Qfractional(by definition of Q)\begin{align*} Q'\cdot b &= b(a_{-2}b^{-1} + a_{-3}b^{-2} + \cdots + a_{-(m-1)}b^{-(m-1)+1}+a_{-m}b^{-m+1}+\cdots) & (\text{by definition of $Q'$})\\[1em] &= a_{-2}b^{0} + a_{-3}b^{-1} \cdots + a_{-(m-1)}b^{-(m-1)+2}+a_{-m}b^{-m+2}+\cdots & (\text{multiply})\\[1em] &= a_{-2} + a_{-3}b^{-1} \cdots + a_{-(m-1)}b^{-(m-1)+2}+a_{-m}b^{-m+2}+\cdots & (\text{since $b^0=1$})\\[1em] &= a_{-2} + (a_{-3}b^{-1} \cdots + a_{-(m-1)}b^{-(m-1)+2}+a_{-m}b^{-m+2}+\cdots) & (\text{rewrite})\\[1em] &= \underbrace{a_{-2}}_{\text{integer}} + \underbrace{Q''}_{\text{fractional}} & (\text{by definition of $Q''$}) \end{align*}

Proceeding in this way, we obtain the digits a1,a2,,ama_{-1}, a_{-2}, \ldots, a_{-m}, but we could go on obtaining digits indefinitely, as the process above indicates. Consider again the following picture (shown previously), which shows how to convert the fractional portion of 14.73 to base 3:

The reason why the procedure above works should now make much more sense logically.

Radix fractions (problems)

info

The following problems are taken from [12].

Fractional numbers can be expressed, in the ordinary scale, by digits following a decimal point. The same notation is also used for other bases; therefore, just as the expression .3012 stands for

3/10+0/102+1/103+2/104,3/10 + 0/10^2 + 1/10^3 + 2/10^4,

the expression (.3012)b(.3012)_b stands for

3/b+0/b2+1/b3+2/b4.3/b + 0/b^2 + 1/b^3 + 2/b^4.

An expression like (.3012)b(.3012)_b is called a radix fraction for base bb. A radix fraction for base 10 is commonly called a decimal fraction.

Radix fraction to decimal fraction

Problem: Show how to convert a radix fraction for base bb into a decimal fraction.

Solution: Consider the following number NN in base bb:

N=\underbrace{a_nb^n+a_{n-1}b^{n-1}+a_{n-2}b^{n-2}+\cdots+a_2b^2+a_1b^1+a_0b^0}_{\text{integer portion}}+\underbrace{a_{-1}b^{-1}+\cdots+a_{-(m-1)}b^{-(m-1)}+a_{-m}b^{-m}+\cdots}_{\text{fractional portion}}.}

To convert a radix fraction for base bb into a decimal fraction, we simply use the expression for the fractional portion above and substitue in the bb-value for whatever base bb we are using. Thus, to express (0.112)3(0.112)_3 as a decimal fraction, we simply write

1(31)+1(32)+2(33)=1427=0.5185185181(3^{-1})+1(3^{-2})+2(3^{-3})=\frac{14}{27}=0.518518518\ldots

Decimal fraction to radix fraction

Problem: Show how to convert a decimal fraction into a radix fraction for base bb.

Solution: To convert a decimal fraction into a radix fraction for base bb, we use the method outlined in the section above (i.e., the section about converting the fractional portion of a number NN into base bb).

Approximate radix fractions as decimal fractions

Problem: Approximate to four places (.3012)4(.3012)_4 and (.3t1e)12(.3t1e)_{12} as decimal fractions.

Solution: Consider the following:

  • (0.3012)4(0.3012)_4: We may approximate out to four decimal places as follows:

    (0.3012)4=3(41)+0(42)+1(43)+2(44)=99128=0.77343750.7734.(0.3012)_4=3(4^{-1})+0(4^{-2})+1(4^{-3})+2(4^{-4})=\frac{99}{128}=0.7734375\ldots\approx 0.7734.
  • (3t1e)12(3t1e)_{12}: We may approximate out to four decimal places as follows:

    (3t1e)12=3(121)+t(122)+1(123)+e(124)=4331728+e20736+t144=0.320550.3206,(3t1e)_{12}=3(12^{-1})+t(12^{-2})+1(12^{-3})+e(12^{-4})=\frac{433}{1728}+\frac{e}{20736}+\frac{t}{144}=0.32055\ldots\approx0.3206,

    when we substitute the values t=10t=10 and e=11e=11 into the fractions above.

Approximate a radix fraction in other bases

Problem: Approximate to four places .4402 as a radix fraction, first for base 7, and then for base 12.

Solution: Consider the following:

  • 0.4402 as a radix fraction for base 7:

    Since we are only instructed to approximate out to four places, the above is satisfactory; of course, we could continue the process and obtain an even better approximation if we wanted.

  • 0.4402 as a radix fraction for base 12:

    As above, we may stop once we have approximated out to four places, thus concluding the problem.