Converting to Base 2

Thomas J. Kennedy

Contents:

In Computer Science we often oscillate between two bases: Base 10 and Base $10_2$ ($10_2 = 2_{10}$).

1 Decimal (Base 10) to Binary (Base 2)

Let us start with powers of 2…

$$ \begin{array}{rrll} \frac{1}{2} &=& 2^{-1} &=& 0.100_{2} &=& 0.0111111\overline{1}_{2} \\ \frac{1}{4} &=& 2^{-2} &=& 0.010_{2} &=& 0.0011111\overline{1}_{2} \\ \frac{1}{8} &=& 2^{-3} &=& 0.001_{2} &=& 0.0001111\overline{1}_{2} \\ \end{array} $$

The third column is not immediately obvious. Let us take a few minutes to prove that:

$$ \begin{array}{rrll} \frac{1}{2} &=& 2^{-1} &=& 0.100_{2} &=& 0.0111111\overline{1}_{2} \\ \end{array} $$

is true. We will need to use the geometric series formula.


We need to prove that

\begin{array}{rl} 0.100_{2} &=& 0.0111111\overline{1}_{2} \\ 2^{-1} &=& 2^{-2} + 2^{-3} + 2^{-4} + \ldots \\ \end{array}

Notice how I changed from based to to base 10 and tweaked the right side? We now have a geometric series! We can apply the geometric series formula (after one small tweak).

\begin{array}{rl} 2^{-1} &=& 2^{-2} + 2^{-3} + 2^{-4} + \ldots \\ 2^{-1} &=& 2^{-2} \left(2^{0} + 2^{-1} + 2^{-2} + \ldots \right) \\ 2^{-1} &=& 2^{-2} \left( \frac{1}{1-2^{-1}} \right) \\ 2^{-1} &=& 2^{-2} \left( \frac{1}{2^{-1}} \right) \\ 2^{-1} &=& 2^{-2} \left( 2 \right) \\ 2^{-1} &=& 2^{-1} \\ \end{array}

2 Converting Real Numbers from Base 10 to Base 2

Example 1

Convert $\frac{1}{5} = 0.2$ to binary.

Step Buffer Bit
1 $2(0.2) = 0.4$ 0
2 $2(0.4) = 0.8$ 0
3 $2(0.8) = 1.6$ 1
4 $2(0.6) = 1.2$ 1
5 $2(0.2) = 0.4$

Starting with step 5, the pattern repeats. We are left with:

$$ \frac{1}{5} = 0.2_{10} = (0.\overline{0011})_{2} $$


Example 2

Convert $\frac{1}{10} = 0.1$ to binary.

Step Buffer Bit
1 $2(0.1) = 0.2$ 0
2 $2(0.2) = 0.4$ 0
3 $2(0.4) = 0.8$ 0
4 $2(0.8) = 1.6$ 1
5 $2(0.6) = 1.2$ 1
6 $2(0.2) = 0.4$

Starting with step 6, the pattern repeats from step (digit) 2. We are left with:

$$ \frac{1}{10} = 0.1_{10} = (0.0\overline{0011})_{2} $$


Example 2

Convert $\frac{1}{7} = 0.\overline{142857}$ to binary.

Step Buffer Bit
1 $2(\frac{1}{7}) = \frac{2}{7}$ 0
2 $2(\frac{2}{7}) = \frac{4}{7}$ 0
3 $2(\frac{4}{7}) = \frac{8}{7}$ 1
4 $2(\frac{1}{7}) = \frac{2}{7}$ 0
5 $2(\frac{2}{7}) = \frac{4}{7}$ 0
6 $2(\frac{4}{7}) = \frac{8}{7}$

Starting with step 6, the pattern repeats from step (digit) 3. We are left with:

$$ \frac{1}{7} = 0.\overline{142857}_{10} = (0.\overline{001})_{2} $$

3 Octal (Base 8) & Hexadecimal (Base 16)

Both Octal and Hexadecimal numbers are shorthand for binary. Octal works with 3 bits and hexadecimal works with 4 bits.

Binary Bits Octal Digit Base 10
000 0 0
001 1 1
010 2 2
011 3 3
100 4 4
101 5 5
110 6 6
111 7 7
Binary Bits Hexadecimal Digit Base 10
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15

Anytime I do back-of-the-envelope conversions in the middle of a problem, these tables are involved.