How do you convert from IEEE 754 single-precision floating-point format to decimal?

StackOverflow https://stackoverflow.com/questions/2406215

  •  18-09-2019
  •  | 
  •  

Question

I understand that the first bit is the sign and that the next 8 bits is the exponent. So in this example you would have 1.1001*2^-4 ? How do I then interpret this in decimal?

0 01111011 10010000000000000000000
Was it helpful?

Solution

Since you already figured out that this is (in binary)1.1001*10^-100, now you just have to convert the binary number to decimal.

In decimal, each digit has a value one-tenth as much as the one before it. In binary, each digit has a value one-half as much as the one before it.

First 1.1001*10^-100 = 0.00011001.

Which is...

  0 * 1           0 * 1
+ 0 * 1/2       + 0 * 0.5
+ 0 * 1/4       + 0 * 0.25
+ 0 * 1/8       + 0 * 0.125
+ 1 * 1/16      + 1 * 0.0625
+ 1 * 1/32      + 1 * 0.03125
+ 0 * 1/64      + 0 * 0.015625
+ 0 * 1/128     + 0 * 0.0078125
+ 1 * 1/256     + 1 * 0.00390625 

0.0625 + 0.03125 + 0.00390625 = 0.09765625

That's all there is to it.

OTHER TIPS

1.1001b is 1*1 + 0.5*1 + 0.25*0 + 0.125*0 + 0.0625*1, or 1.5625. Multiply that by 2**-4 (0.0625) to get 0.09765625.

in C:

int fl = *(int*)&floatVar;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top