This subsection discusses the conversion process between decimal and binary numbers.
The conversion from binary is actually simpler than you might think at first. There are five main steps:
Here is an example of the conversion of the binary number 1110 0010:
Place 20 above the right most bit
20 | |||||||
1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 |
Place 2's above all of the bits, like in step 1 (just without the exponent).
2 | 2 | 2 | 2 | 2 | 2 | 2 | 20 |
1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 |
Starting from the right, place exponents on the 2's so that it counts up from the right.
27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 |
Multiply each bit by the power of 2 above it.
Powers of 2: | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
Bits: | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 |
Multiplied result: | 27 | 26 | 25 | 0 | 0 | 0 | 21 | 0 |
Add all of the results from the previous step and the result will be the decimal equivalent.
$ \text{Decimal Equivalent} = 2^7 + 2^6 + 2^5 + 2^1 = 128 + 64 + 32 + 2 = 226 $
A table of powers of two can help with conversions and further cement that bit positions have solid values.
The conversion process from decimal to binary is a little more involved since the faster (more efficient) way requires the use of logarithms, but there is a simpler but more involved process where you look for the largest power of 2 in the number. The 8 steps for some decimal number $D$, using the logarithm method, are:
An example conversion should help clear up how the process works. We will convert 192 into binary:
Since $D$ is now $0$, we will arrange our written numbers from left to right, largest to smallest, and spaces for numbers skipped. (Remember that 0 should be included/is the last number).
7 | 6 |
Write 1's below the values written in, and 0's for the blank spaces.
7 | 6 | ||||||
1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
The binary number is the sequence of bits you just filled in (in step 7). This result can be confirmed with an online binary converter. Such as the one here.
The other method of converting from decimal to binary is by finding the largest power of 2 in that number, and then subtracting it, and repeating until the result is 0. Recording each power until we stop. This is the more intuitive method that may be easier, especially with a table of the powers of 2 to help speed the process up. Like the table above(which only goes up to $2^8$).
The main process of this method is as follows:
Using the previous example number of 192, we will find the highest power of 2 that fit into it. This power of 2 is $2^7$, which is 128.
We subtract the current number, 192, by the power of 2 we found to get our next current number.
$192-128=64$
Write down the power of 2 we used and go back to step 1 until we hit 0.
$7$
We find that the highest power of 2 that fits into 64, is 64, which is $2^6$.
We subtract our numbers to get the next number.
$64-64=0$
We write down the power of 2, and break out of our loop since we hit 0.
$7 6$
Write out the powers of two we used, leaving blanks for the numbers we didn't use.
$7$ | $6$ |
Write 1's under the powers of 2 that are filled in, and 0's under the blank spaces.
$7$ | $6$ | ||||||
1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
This number is 1100 0000 and can be confirmed by the process we used above, and in the binary converter that is also linked above as well.