Converting to Base 2
Thomas J. Kennedy
In Computer Science we often oscillate between two bases: Base 10 and Base 102 (102=210).
1 Decimal (Base 10) to Binary (Base 2)
Let us start with powers of 2…
12=2−1=0.1002=0.0111111¯1214=2−2=0.0102=0.0011111¯1218=2−3=0.0012=0.0001111¯12
The third column is not immediately obvious. Let us take a few minutes to prove that:
12=2−1=0.1002=0.0111111¯12
is true. We will need to use the geometric series formula.
We need to prove that
0.1002=0.0111111¯122−1=2−2+2−3+2−4+…
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).
2−1=2−2+2−3+2−4+…2−1=2−2(20+2−1+2−2+…)2−1=2−2(11−2−1)2−1=2−2(12−1)2−1=2−2(2)2−1=2−1
2 Converting Real Numbers from Base 10 to Base 2
Example 1
Convert 15=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:
15=0.210=(0.¯0011)2
Example 2
Convert 110=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:
110=0.110=(0.0¯0011)2
Example 2
Convert 17=0.¯142857 to binary.
Step | Buffer | Bit |
---|---|---|
1 | 2(17)=27 | 0 |
2 | 2(27)=47 | 0 |
3 | 2(47)=87 | 1 |
4 | 2(17)=27 | 0 |
5 | 2(27)=47 | 0 |
6 | 2(47)=87 | – |
Starting with step 6, the pattern repeats from step (digit) 3. We are left with:
17=0.¯14285710=(0.¯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.