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