Department of Energy Argonne National Laboratory Office of Science NEWTON's Homepage NEWTON's Homepage
NEWTON, Ask A Scientist!
NEWTON Home Page NEWTON Teachers Visit Our Archives Ask A Question How To Ask A Question Question of the Week Our Expert Scientists Volunteer at NEWTON! Frequently Asked Questions Referencing NEWTON About NEWTON About Ask A Scientist Education At Argonne 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

NEWTON is an electronic community for Science, Math, and Computer Science K-12 Educators, sponsored and operated by Argonne National Laboratory's Educational Programs, Andrew Skipor, Ph.D., Head of Educational Programs.

For assistance with NEWTON contact a System Operator (help@newton.dep.anl.gov), or at Argonne's Educational Programs

NEWTON AND ASK A SCIENTIST
Educational Programs
Building 360
9700 S. Cass Ave.
Argonne, Illinois
60439-4845, USA
Update: June 2012
Weclome To Newton

Argonne National Laboratory