Converting from Decimal to Binary
Converting from decimal to binary doesn't take any difficult
math, but the algorithm is a little longer. In this section, you'll read about a
general algorithm for converting from decimal to binary, followed by a couple of
examples.
The general algorithm is as follows:
|
Step 1. |
Find the decimal power of 2 that is closest to, but not
larger than, the decimal number being converted.
|
|
Step 2. |
Write down a binary 1 as the left-most binary digit of the
new binary number.
|
|
Step 3. |
Subtract that power of 2 from the original decimal number.
(The resulting number will be called the remainder in this
algorithm.) |
|
Step 4. |
Repeat the following steps until the steps have been
completed for the case in which the power of 2 is 1 (2 0). As a result
of passing through these steps repeatedly, another binary digiteither 0 or 1will
be added to the right side of the binary number for each pass through this
step: - a. If the next lower power of 2 (compared to the previous step) is
larger than the remainder, write down a binary 0 as the next binary
digit.
- b. Otherwise, write down a binary 1 as the next digit. Also, subtract
the current power of 2 from the remainder, with the resulting remainder being
used as the remainder in the next pass through Step
4.
|
To appreciate how to use this algorithm, you need to either
know the powers of 2 or have them listed somewhere handy. Table B-5 lists the powers of 2 that will be used in
this appendix.
The first example will be the conversion of decimal 235 into
binary 11101011. To make the conversion happen, the following describes the
first 3 steps of the algorithm:
|
1. |
The closest power of 2 not bigger than 235 is
128.
|
|
2. |
The first binary digit will be 1.
|
|
3. |
The remainder is calculated as 235 128 = 107. The first pass
through Step 4 of the algorithm will use this remainder
value.
|
Although describing these first three steps might be helpful,
putting the same information into a table can help as well. Because the eventual
binary number will be an eight-digit binary number in this example, the table
will have places for eight digits. (When you use this algorithm, you might not
know how many digits the new binary number will have, so leave plenty of space
on the right side of your paper.) Table
B-6 shows the details of the first three steps.