Integer representation as floating, clarification needed

Take

int x = 5;
float y = x;

//"I know it a float .. you know it a float .. but take it address
// and pretend you're looking at an integer and then dereference it"

printf("%d\n", *(int*)&y); //1084227584

Why am I seeing this number?

  • 5 in binary format 0101
  • 5 can be represented as (1.25 * 2^2), which means that

It can be represented as:

[sign bit]                              - 0
[8 bits worth of exp] - 129 (129-127=2) - 1000|0001
[23 bits of .xxxxxxx] - 25              - 1100|1

Connecting, I have

[sign bit][8 bits worth of exp][23 bits worth of .xxx]
0         10000001             11001000000000 //2126336

What am I missing, please?

+3
source share
2 answers

Others have indicated that it is not portable ... but you already know this, and you specified the 64-bit OS X. In principle, you have the mantissa incorrectly. 1.25appears to be the implicit leading bit for 1.0. The first explicit bit of the mantissa represents the 0.5second bit 0.25. So, the mantissa is actually: 01000000000000000000000.

0 10000001, , : 0x40a00000, 1084227584 .

+3

?. float int.

, , , , , - undefined. ints ? , ?

0

All Articles