Sub topic 9
Sign, Biased Exponent and Mantissa Fields
This chapter assumes you can write binary fractions, such as 0.101b = 0.625, and normalize a number into the form 1.xxx × 2^e. If not, review the previous chapters on fractional conversion and normalized scientific form first.
The three fields
A floating-point number is stored as a bit string split into three fields.
| Field | Width (single) | Width (double) | Job |
|---|---|---|---|
| Sign (S) | 1 | 1 | 0 for positive, 1 for negative |
| Exponent (E) | 8 | 11 | Stored as an unsigned integer with a bias added |
| Mantissa (fraction, F) | 23 | 52 | Bits to the right of the binary point in the normalized significand |
For a normal number, the decoded value is:
value = (-1)^S × 1.F × 2^(E − bias)
The sign is stored separately from the magnitude, as in sign-magnitude integers. This is why floating point has both +0 and −0, a point the special-values chapter covers.
Terminology varies. The standard calls F the fraction, and the full 1.F the significand. Many textbooks and GATE questions call F the mantissa. In this chapter, the mantissa is the stored fraction bits.
Why the exponent is biased
The exponent must be negative for numbers below 1 and positive for numbers above 1. Storing it in 2's complement would work for arithmetic, but it makes comparison awkward. A stored exponent with 2's complement would make 2^-3 look "larger" than 2^3 when compared as raw bits.
The fix is to add a constant, the bias, to the true exponent before storing it:
E = e + bias, where bias = 2^(k−1) − 1 for a k-bit exponent field.
All stored exponents are then non-negative, and a bigger true exponent always gives a bigger stored value. For positive floats, comparing the raw bit patterns as unsigned integers gives the same order as comparing the numbers. Hardware can sort or compare floats with an integer comparator.
| Format | k | bias | Normal E range | True exponent range |
|---|---|---|---|---|
| Half | 5 | 15 | 1 to 30 | −14 to 15 |
| Single | 8 | 127 | 1 to 254 | −126 to 127 |
| Double | 11 | 1023 | 1 to 2046 | −1022 to 1023 |
The patterns E = 0 and E = all ones are not normal numbers. They are reserved for zero, denormals, infinity and NaN, so normal exponents run from 1 to 2^k − 2.
The hidden bit
In normalized binary form, the leading digit is always 1. Since it is always 1, the format does not store it. This implicit leading 1 is the hidden bit. A 23-bit fraction field therefore gives 24 bits of precision, and a 52-bit field gives 53.
Encoding step by step
To encode a decimal number:
- Convert it to binary and set S from its sign.
- Normalize it to 1.xxx × 2^e.
- Compute E = e + bias and write it in k bits.
- Take the digits after the point as F and pad with zeros on the right to the fraction width.
Example: encode 20.75 in single precision.
- 20.75 = 10100.11b, and S = 0.
- Normalize: 1.010011 × 2^4, so e = 4.
- E = 4 + 127 = 131 = 10000011b.
- F = 010011 followed by 17 zeros, for 23 bits.
The 32 bits are 0 10000011 01001100000000000000000. Regrouped in fours, that is 0100 0001 1010 0110 0000 0000 0000 0000, which is 0x41A60000.
Encoding and decoding in code
The functions below work for any exponent width k and fraction width m, which is useful for GATE's custom formats. They handle only exactly representable, normal, non-zero values.
import math
def encode(x, k, m):
"""Return (S, E, F) for x in a format with k exponent bits and m fraction bits."""
if x == 0:
raise ValueError("zero uses a reserved pattern")
bias = 2 ** (k - 1) - 1
s = 1 if x < 0 else 0
mant, e = math.frexp(abs(x)) # abs(x) = mant * 2**e, 0.5 <= mant < 1
sig, e = mant * 2, e - 1 # now 1 <= sig < 2
scaled = (sig - 1) * 2 ** m
if scaled != int(scaled):
raise ValueError("needs more than m fraction bits")
E = e + bias
if not 1 <= E <= 2 ** k - 2:
raise OverflowError("exponent outside the normal range")
return s, E, int(scaled)
def decode(s, E, f, k, m):
bias = 2 ** (k - 1) - 1
return (-1) ** s * (1 + f / 2 ** m) * 2 ** (E - bias)
s, E, f = encode(20.75, 8, 23)
print(s, format(E, '08b'), format(f, '023b'))
# 0 10000011 01001100000000000000000
print(decode(s, E, f, 8, 23)) # 20.75
s, E, f = encode(-6.5, 5, 10) # a custom half-precision-style format
print(s, format(E, '05b'), format(f, '010b'))
# 1 10001 1010000000
print(decode(s, E, f, 5, 10)) # -6.5For -6.5, we have −6.5 = −110.1b = −1.101 × 2^2. With k = 5 the bias is 15, so E = 17 = 10001b.
Decoding by hand
Decode 0xC1280000 in single precision.
- In binary, this is 1100 0001 0010 1000 0000 ....
- Split into 1 | 10000010 | 0101000....
- S = 1 means negative. E = 130, so e = 130 − 127 = 3. F = 0101, so the significand is 1.0101b = 1.3125.
- Value = −1.3125 × 2^3 = −10.5.
Reading limits from field widths
The field widths determine the extreme values. For a format with k exponent bits and m fraction bits:
- Largest normal: (2 − 2^−m) × 2^(2^k − 2 − bias)
- Smallest positive normal: 2^(1 − bias)
- Precision: m + 1 significant bits with a hidden bit.
For single precision, the largest normal is (2 − 2^−23) × 2^127 ≈ 3.4 × 10^38. The smallest positive normal is 2^−126 ≈ 1.18 × 10^−38.
For a custom GATE format with 1 sign bit, 6 exponent bits, 9 fraction bits and bias 31, the normal E range is 1 to 62. The true exponent therefore runs from −30 to 31, and the largest normal value is (2 − 2^−9) × 2^31.
Common mistakes
- Using the exponent as a 2's complement number, for example reading 10000011 as −125 rather than 131 − 127 = 4.
- Left-padding the fraction. Fraction bits are padded with zeros on the right, because they sit after the binary point.
- Normalizing numbers below 1 incorrectly. 0.1101b = 1.101 × 2^−1, so E = 126 in single precision.
- Counting only 23 bits of precision for single. With the hidden bit, it is 24.
- Applying the normal formula to E = 0 or E = all ones. These patterns follow different rules.
Exercise
Encode −10.5 in IEEE 754 single precision and give the answer in hexadecimal.
Show answer
−10.5 = −1010.1b = −1.0101 × 2^3. S = 1, E = 3 + 127 = 130 = 10000010b, and F = 0101 followed by 19 zeros. The bits are 1 10000010 01010000000000000000000, which is 0xC1280000.
Exercise
A format has 1 sign bit, 6 exponent bits with bias 31, and 9 fraction bits with a hidden bit. Exponent patterns 0 and 63 are reserved. What is the smallest positive normal number and the largest normal number?
Show answer
The normal E range is 1 to 62, so the true exponent runs from −30 to 31. The smallest positive normal is 1.0 × 2^−30, since F = 0. The largest normal is (2 − 2^−9) × 2^31.