Java Reference
In-Depth Information
Conversions
We've already seen how a number in another base is converted to decimal by
determining the place value of each digit and computing the result. This process
can be used to convert any number in any base to its equivalent value in base 10.
Now let's reverse the process, converting a base-10 value to another base. First,
find the highest place value in the new number system that is less than or equal to
the original value. Then divide the original number by that place value to deter-
mine the digit that belongs in that position. The remainder is the value that must
be represented in the remaining digit positions. Continue this process, position by
position, until the entire value is represented.
For example, Figure B.5 shows the process of converting the decimal value
180 into binary. The highest place value in binary that is less than or equal to
180 is 128 (or 2 7 ), which is the eighth bit position from the right. Dividing 180
by 128 yields 1 with 52 remaining. Therefore the first bit is 1, and the decimal
value 52 must be represented in the remaining seven bits. Dividing 52 by 64,
which is the next place value (2 6 ), yields 0 with 52 remaining. So the second bit
is 0. Dividing 52 by 32 yields 1 with 20 remaining. So the third bit is 1, and the
remaining five bits must represent the value 20. Dividing 20 by 16 yields 1 with
4 remaining. Dividing 4 by 8 yields 0 with 4 remaining. Dividing 4 by 4 yields 1
with 0 remaining.
Since the number has been completely represented, the rest of the bits are zero.
Therefore 180 10 is equivalent to 10110100 in binary. This can be confirmed by
Place
value
Number
Digit
128
180
1
64
52
0
32
52
1
180 10 = 10110100 2
16
20
1
8
4
0
4
4
1
2
0
0
1
0
0
FIGURE B.5 Converting a decimal value into binary
 
Search WWH ::




Custom Search