Jose Ortiz - 2 months ago 15

C Question

I cannot figure out to convert the value of a referenced

`float`

`float`

`#include <stdio.h>`

main() {

int i;

float *f;

i = 1092616192;

f = (float *)&i;

printf("i is %d and f is %f\n", i, *f);

}

the output for

`f`

`10`

Answer

Normally, the value of 1092616192 in hexadecimal is 0x41200000.

In floating-point, that will give you:

```
sign = positive (0b)
exponent = 130, 2^3 (10000010b)
significand = 2097152, 1.25 (01000000000000000000000b)
```

2^3*1.25 = 8 *1.25 = 10

To explain the exponent part uses an offset encoding, so you have to subtract 127 from it to get the real value. 130 - 127 = 3. And since this is a binary encoding, we use 2 as the base. 2 ^ 3 = 8.

To explain the significand part, you start with an invisible 'whole' value of 1. the uppermost (leftmost) bit is half of that, 0.5. The next bit is half of 0.5, 0.25. Because only the 0.25 bit and the default '1' bit is set, the significand represents 1 + 0.25 = 1.25.

Source (Stackoverflow)

Comments