Sub topic 10
IEEE 754 Single and Double Precision: Encoding and Decoding
This chapter assumes you can convert fractions between decimal and binary, write a number in normalized binary scientific form (1.f × 2^k), and understand why a biased exponent is used. Here you learn to encode a decimal value into the 32-bit and 64-bit IEEE 754 formats and to decode a bit pattern back into a value.
Field layout
Both formats split the word into three fields, from the most significant bit down: sign, biased exponent, fraction.
| Format | Total bits | Sign | Exponent | Fraction | Bias |
|---|---|---|---|---|---|
| Single (binary32) | 32 | 1 | 8 | 23 | 127 |
| Double (binary64) | 64 | 1 | 11 | 52 | 1023 |
The bias is 2^(k-1) - 1 for a k-bit exponent field. The fraction stores only the bits after the binary point. A normalized significand always starts with 1, so that leading 1 is implicit (the "hidden bit"). Single precision therefore has 24 significant bits and double has 53.
Decoding a normal number
If the exponent field e is neither all zeros nor all ones, the value is:
value = (-1)^s × (1 + f / 2^m) × 2^(e - bias)
Here m is the fraction width (23 or 52), and f is the fraction field read as an unsigned integer.
The reserved exponent values are the all-zeros and all-ones patterns. They are used for zero, denormals, infinity and NaN, which a later chapter covers. So normal numbers use only these exponent fields:
- Single: e from 1 to 254, giving true exponents -126 to +127.
- Double: e from 1 to 2046, giving true exponents -1022 to +1023.
Worked decode
Decode 0xC1480000.
- In binary this is
1100 0001 0100 1000 0000 .... - The sign is 1, so the number is negative.
- The next 8 bits are
10000010, which is 130. The true exponent is 130 - 127 = 3. - The fraction is
1001000.... The significand is 1.1001b = 1.5625. - The value is -1.5625 × 2^3 = -12.5.
Encoding a decimal number
Follow these steps:
- Record the sign bit and work with the absolute value.
- Convert the integer and fractional parts to binary.
- Normalize to 1.xxx × 2^k by moving the binary point.
- Add the bias to k to get the exponent field.
- Take the bits after the leading 1 as the fraction. Pad with zeros on the right, or round if there are more bits than fit.
- Concatenate sign, exponent and fraction, then group into hex digits.
Worked encode: 10.375 in single precision
- 10 = 1010b and 0.375 = 0.011b, so 10.375 = 1010.011b.
- Normalizing gives 1.010011 × 2^3.
- The exponent field is 3 + 127 = 130 =
10000010. - The fraction is
010011followed by 17 zeros, which makes 23 bits. - The sign is 0, so the pattern is
0 10000010 01001100000000000000000. - Regrouped in fours this is
0100 0001 0010 0110 0000 ..., so the hex value is0x41260000.
The same number in double precision
Only the widths and bias change. For -6.5 = -110.1b = -1.101b × 2^2:
- Single: e = 2 + 127 = 129 =
10000001, giving0xC0D00000. - Double: e = 2 + 1023 = 1025 =
10000000001, giving0xC01A000000000000.
Checking your work in Python
import struct
def fields(x, double=False):
if double:
bits = struct.unpack('>Q', struct.pack('>d', x))[0]
e_bits, f_bits, bias = 11, 52, 1023
else:
bits = struct.unpack('>I', struct.pack('>f', x))[0]
e_bits, f_bits, bias = 8, 23, 127
s = bits >> (e_bits + f_bits)
e = (bits >> f_bits) & ((1 << e_bits) - 1)
f = bits & ((1 << f_bits) - 1)
return bits, s, e, f, bias
for x, dbl in [(10.375, False), (-6.5, False), (-6.5, True)]:
bits, s, e, f, bias = fields(x, dbl)
print(hex(bits), s, e, format(f, 'x'), e - bias)
# 0x41260000 0 130 260000 3
# 0xc0d00000 1 129 500000 2
# 0xc01a000000000000 1 1025 a000000000000 2The struct module packs a Python float into the exact IEEE bytes. The '>' forces big-endian order, so the printed hex matches the bit layout in this chapter.
When the fraction does not fit
Many decimal values have infinite binary expansions. Take 0.1, which is 1.1001100110011...b × 2^-4 in single precision.
- The exponent field is -4 + 127 = 123 =
01111011. - The first 23 fraction bits are
10011001100110011001100. The next bits are1 1001..., which is more than half a unit in the last place. - The default rounding mode (round to nearest, ties to even) rounds up, so the fraction becomes
10011001100110011001101. - The final pattern is
0x3DCCCCCD.
This stored value is slightly above 0.1. The same effect in double precision is why 0.1 + 0.2 == 0.3 is false in Python.
Range and precision
| Quantity | Single | Double |
|---|---|---|
| Largest finite | (2 - 2^-23) × 2^127 ≈ 3.40 × 10^38 | (2 - 2^-52) × 2^1023 ≈ 1.80 × 10^308 |
| Smallest positive normal | 2^-126 ≈ 1.18 × 10^-38 | 2^-1022 ≈ 2.23 × 10^-308 |
| Machine epsilon | 2^-23 ≈ 1.19 × 10^-7 | 2^-52 ≈ 2.22 × 10^-16 |
| Decimal digits | about 7 | about 15 to 16 |
Machine epsilon is the gap between 1.0 and the next representable number. The spacing between neighbours doubles every time the exponent increases by one. So the absolute error grows with magnitude, while the relative error stays at most about 2^-24 for single and 2^-53 for double.
Common errors
- Forgetting the hidden bit. The significand is 1.f, not 0.f. Leaving out the 1 gives an answer that is wrong by a factor between 1 and 2.
- Padding on the wrong side. Fraction bits are left-aligned, so pad zeros on the right. A fraction of
101is10100...0, not0...0101. - Using the wrong bias. Use 127 for single and 1023 for double. If the exponent is negative, add the bias to the signed value, for example -4 + 127 = 123.
- Treating the exponent field as two's complement. It is an unsigned integer, and the bias makes it comparable as an unsigned number. Positive floats therefore sort in the same order as their bit patterns.
- Moving the point the wrong way. Moving the point left by k places gives +k in the exponent. Moving it right gives -k.
- Using 0 and 255 as normal exponents. In single precision, e = 0 and e = 255 are reserved. In double precision, e = 0 and e = 2047 are reserved.
Custom formats in exam questions
GATE questions often give a nonstandard layout, such as 1 sign bit, 6 exponent bits and 9 fraction bits. Apply the same rules:
- The bias is 2^(6-1) - 1 = 31 unless the question states another bias.
- Normal exponent fields run from 1 to 2^6 - 2 = 62, so the true exponent runs from -30 to +31.
- The significand is 1 + f / 2^9, with a 10-bit significand including the hidden bit.
- Read the question for whether the leading 1 is hidden. Some questions use an explicit leading bit or a normalized form 0.1f × 2^k, which shifts both the range and the bias arithmetic.
Exercise
Encode -0.15625 as a single-precision number and give the result in hex.
Show answer
0.15625 = 0.00101b = 1.01b × 2^-3. The exponent field is -3 + 127 = 124 = 01111100. The fraction is 01 followed by 21 zeros. The sign is 1. The pattern is 1 01111100 01000000000000000000000 = 1011 1110 0010 0000 ... = 0xBE200000.
Exercise
A format has 1 sign bit, 5 exponent bits with bias 15, and 10 fraction bits, with a hidden leading 1. What is the largest finite value, assuming the all-ones exponent is reserved?
Show answer
The largest normal exponent field is 2^5 - 2 = 30, so the true exponent is 30 - 15 = 15. The largest significand is 2 - 2^-10. The largest finite value is (2 - 2^-10) × 2^15 = 65504.