Converting bit patterns to IEEE 754 Single Precision equivalents
By nicomp
Converting bit patterns to IEEE 754 Single Precision equivalents
The IEEE 754 standard defines how floating point numbers are stored in binary form. In this tutorial we convert a bit pattern into its' base 10 equivalent.
Test Case
Given a 32-bit base 2 number:
0100 0010 0010 1001 1110 0100 0000 00002
This bit pattern has no inherent encoding, but if we assume that it represents an IEEE 754 single precision number, we can convert it to base 10.
Sign
| Exponent (biased 127)
| Fraction (without the leading '1')
|
|---|---|---|
0
| 100 0010 0
| 010 1001 1110 0100 0000 0000
|
Calculate the exponent
Exponent = 128 + 4 = 132
Subtracting the bias of 127, we get 5. Therefore we will need to shift the fraction 5 places to the right.
Calculate the Fraction
Fraction = 1.010 1001 1110 0100 0000 0000 (base 2)
The leading '1' is not stored in the original bit pattern. According to the IEEE 754 standard, a leading '1' is always assumed.
Apply the 'hidden' digit to the right of the radix point
One solution is 1.010 1001 1110 0100 0000 00002 X 25
This solution is unwieldy since it is in base 2.
Apply the exponent to the fraction by sliding the radix point
Another solution is 1010 10.01 1110 0100 0000 0000
This solution is unwieldy since it is in base 2.
Convert the resulting bit pattern to powers of 2
Another solution is (20+2-2+2-4+2-7+2-8+2-9+2-10+2-13) * 25
Apply the exponent to the powers of 2
Another solution is (25+23+21+2-2+2-3+2-4+2-5+2-8)
Converting each position to base 10 and adding gives us:
32.
8.
2.
.25
.125
.0625
.03125
.00390625 ============
42.47265625
Therefore,
0100 0010 0010 1001 1110 0100 0000 00002 converts to = 42.4726562510
ICnow 2 years ago
Awesome! Best explanation on the web! Perfect!