- Binary-to-decimal conversion:

and

1 *x* ** <**- *d _{k}* (move leftmost digit to x)

2

3

4

5

6

**NOTE:** If this conversion algorithm were performed in some other number system (such as octal - base 8, or quinary - base 5) then the resulting representation would be IN THAT BASE system:

For base 5 (quinary) to decimal:

- Decimal-to-binary conversion:
is based on repeated division by 2 in decimal, so that the binary digits generated from right to left (

__using decimal arithmetic__):Assume representation to be converted is associated with the variable

*x*:1

*s***<**- 0 (starting from rightmost digit)

2**while***x*!=**0**do

3*d*_{s }**<**-*x*mod 2 (get remainder of divide by 2)

4*x***<**- floor(*x*/2) (reset x to quotient)

5*s***<**-*s*+ 1 (set up for next digit)

6**stop**[**]**WHY DOES THIS WORK?****Because - the "mod" function gives us back the remainder of the division of the number by 2, and this is the low order digit. That is:****(***d*2_{k}^{k}+ d_{k-1}2^{k- 1}+....+*d*_{1}2^{1}+*d*_{0}2^{0}) mod 2 =(2 (

*d*2_{k}^{k-1}+ d_{k-1}2^{k- 2}+....+*d*_{1}2^{0}) +*d*_{0}2^{0}) mod 2 =*d*2_{0}^{0}Through the repeated application of the

**mod**function to the quotient of the previous step, we successively get the successive digits of the binary representation.For more help on number conversions see:

A Base Converter

CS1104 Main Page

Last Updated 2002/02/04

© L.Heath, 2000, modified by J.A.N. Lee