 |
 |
Binary to decimal
Name: peter j hagdahl
Status: N/A
Age: N/A
Location: N/A
Country: N/A
Date: Around 1995
Question:
I am in desperate need of a way to convert between binary floating point
and decimal floating point arithmetics.
Replies:
I am assuming you want to do these calculations by hand. Here is a way I
was taught to convert from decimal to binary:
For integers (or the integer part of a number), divide repeatedly by 2,
using "remainders" rather than decimal places, until you get down to 1. For
example, convert decimal 49 to binary:
49/2 = 24 r 1
24/2 = 12 r 0
12/2 = 6 r 0
6/2 = 3 r 0
3/2 = 1 r 1
1/2 = 0 r 1
Now read the remainders from bottom to top: the
binary equivalent is 110001
To convert numbers between 0 and 1, take the decimal expression and repeat-
edly multiply it by 2. At each step, keep track of the integer part of the
result but do not carry it along in subsequent multiplications. For
example, convert decimal 0.7 to binary:
0.7 * 2 = 1.4
0.4 * 2 = 0.8
0.8 * 2 = 1.6
0.6 * 2 = 1.2
0.2 * 2 = 0.4
0.4 * 2 = 0.8
0.8 * 2 = 1.6
0.6 * 2 = 1.2 etc.
Note that we have started to repeat previous
results. Now read the integer parts occurring on
the right side, from the top down: the binary
representation of decimal 0.7 is 0.1011001100...
where the "1100" repeats forever.
The straightforward way of converting binary to decimal is the only way I
know. For the integer part, you simply add up the appropriate powers of 2,
and for the fractional part you add up the appropriate powers of 1/2.
Example: convert binary 1101001 to decimal:
1 + 8 + 32 + 64 = 105
Example: convert binary 0.1101001 to decimal :
.5 + .25 + .0625 + .0078125 = .8203125
(or equivalently, treat the fractional representation as though it were an
integer, do the conversion as shown above, then divide by 2 raised to the
power of the number of digits in the representation: for this example, we
get 105/128 which does equal .8203125).
rcwinther
You can also convert from binary to decimal by reversing the method given
for going from decimal to binary. That is, let ans start at 0 and suppose
the binary number is 11010111. Then compute new values of ans as shown
below:
ans * 2 + 1 =>ans
ans * 2 + 1 =>ans
ans * 2 + 0 =>ans
ans * 2 + 1 =>ans
ans * 2 + 0 =>ans
ans * 2 + 1 =>ans
ans * 2 + 1 =>ans
ans * 2 + 1 =>ans
At this point, ans contains the converted number. That method is especially
good for use with a calculator or computer. There is a similar procedure
for fractions except you must go right to left and divide by 2 rather than
multiply.
chaffer
Click here to return to the Mathematics Archives
| |
Update: June 2012
|
|