How to represent FLOAT number in memory in C

2019-01-01 10:47发布

While reading a tutorial I came across how to represent Float number in memory. The tutorial had an example with a floating point number.

   float a=5.2  with below Diagram

enter image description here

Can anyone please tell how this 5.2 is converted in to binary and how it is represented in memory in above the above diagram?

11条回答
伤终究还是伤i
2楼-- · 2019-01-01 10:49

The conversion technique posted originally on the other website is shown unnecessarily complex (although it takes us to right answer) . For memory representation of 5.2 in memory:

First convert it into simple binary system, which will give us 101.001100110011001100110011

Now change it into scientific form : 1.01001100110011001100110011 x 10^2 .

Now our sign bit is 0 as the number is positive

For exponent we need (127 + 2) upto 8 bits which gives us 10000001

Fraction is 01001100110011001100110 . (23 bits) (Discarding the leading 1 of scientific form)

=> the representation is

0 10000001 0100 1100 1100 1100 1100 110

查看更多
人气声优
3楼-- · 2019-01-01 10:52

5.2 is represented as "01000000101001100110011001100110"

Check the Converter Applet

查看更多
与风俱净
4楼-- · 2019-01-01 10:53

Raw float 5.2:

01000000101001100110011001100110
^ sign bit

In memory, reverse byte order (as your diagram):

01100110011001101010011001000000
                        ^ sign bit
查看更多
只若初见
5楼-- · 2019-01-01 10:54

I think the diagram is not one hundret percent correct.

Floats are stored in memory as follows:

They are decomposed into:

  • sign s (denoting whether it's positive or negative) - 1 bit
  • mantissa m (essentially the digits of your number - 24 bits
  • exponent e - 7 bits

Then, you can write any number x as s * m * 2^e where ^ denotes exponentiation.

5.2 should be represented as follows:

0 10000001 01001100110011001100110    
S    E               M

S=0 denotes that it is a positive number, i.e. s=+1

E is to be interpreted as unsigned number, thus representing 129. Note that you must subtract 127 from E to obtain the original exponent e = E - 127 = 2

M must be interpreted the following way: It is interpreted as a number beginning with a 1 followed by a point (.) and then digits after that point. The digits after . are the ones that are actually coded in m. We introduce weights for each digit:

bits in M: 0   1    0     0      1       ... 
weight:    0.5 0.25 0.125 0.0625 0.03125 ... (take the half of the previous in each step)

Now you sum up the weights where the corresponding bits are set. After you've done this, you add 1 (due to normalization in the IEEE standard, you always add 1 for interpreting M) and obtain the original m.

Now, you compute x = s * m * 2^e and get your original number.

So, the only thing left is that in real memory, bytes might be in reverse order. That is why the number may not be stored as follows:

0 10000001 01001100110011001100110    
S    E               M

but more the other way around (simply take 8-bit blocks and mirror their order)

01100110 01100110 10100110 01000000
MMMMMMMM MMMMMMMM EMMMMMMM SEEEEEEE
查看更多
回忆,回不去的记忆
6楼-- · 2019-01-01 10:54

5.2 in binary 101.00110011...... ------> non normalized form 5.2 is .10100110011.... x 2^3 ------> explicit normal form 5.2 is .0100110011 x 2^3 in implicit normal form

here sign bit becomes 0 (because the number is positive) and exponent is seven bit so it is using excess 64 exponent notation so exponent will become 64+3 = 69 ie 1000101 and remaining will be mantissa (total 32bit - 7 exponent bit - 1 sign bit = 24 bit) 0100 1100 1100 1100 1100 1100

In the above example sign bit is correct Excess 64 is not applied , so not normalized but ideally it should use implicit normalization Mantissa part in second byte if you apply implicit normalization the MSB '1' will not come .

查看更多
还给你的自由
7楼-- · 2019-01-01 11:00
int a;
float b=5.2;
memcpy(&a, &b, 4);
printf("%d",a);

This gives 0100 0000 1010 0110 0110 0110 1000 0001 (1084647041)

查看更多
登录 后发表回答