Sub topic 6
Fixed-point representation
Fixed-point stores a real number as an ordinary integer and remembers, outside the data, where the binary point sits. This chapter assumes you can already convert fractions between decimal and binary, read and write 2's complement integers, and handle powers of two. It covers encoding, decoding, ranges, resolution and arithmetic. It also covers the mistakes that cost marks in numerical questions.
The core idea: an integer with an implied point
A fixed-point number of w bits with f fraction bits is an integer n that represents the value n / 2^f. The hardware sees only the integer. The binary point is a convention shared by the programmer, so no bit is spent on it.
For example, take 8 bits with f = 4. The pattern 0101 0110 is read as 0101.0110:
- Integer part:
0101= 5. - Fraction part:
.0110= 1/4 + 1/8 = 0.375. - Value: 5.375. As a check, the raw integer is 86 and 86 / 16 = 5.375.
Signed fixed-point uses 2's complement for the whole word. The weight of the top bit is -2^(w-1-f), and the other weights are positive powers of two down to 2^-f.
Notation and format parameters
Texts differ on whether the sign bit counts as an integer bit, so read the question closely. This chapter uses these symbols:
wis the total number of bits.fis the number of fraction bits.- For a signed word,
w - 1 - fbits hold the magnitude of the integer part, plus the sign bit.
| Quantity | Unsigned | 2's complement |
|---|---|---|
| Smallest value | 0 | -2^(w-1-f) |
| Largest value | (2^w - 1) / 2^f | (2^(w-1) - 1) / 2^f |
| Resolution (step) | 2^-f | 2^-f |
| Count of distinct values | 2^w | 2^w |
The resolution is the gap between neighbouring values. It is the same everywhere in the range. Here are a few signed 8-bit layouts:
| Fraction bits `f` | Smallest | Largest | Resolution |
|---|---|---|---|
| 0 | -128 | 127 | 1 |
| 2 | -32 | 31.75 | 0.25 |
| 4 | -8 | 7.9375 | 0.0625 |
| 7 | -1 | 0.9921875 | 0.0078125 |
Moving the point left by one place halves the range and halves the step. The number of representable values stays at 2^8 = 256.
Encoding and decoding
To encode a real number x:
- Multiply
xby2^f. - Round to the nearest integer. Truncation is another choice, but you must state it.
- Check that the integer fits in the
w-bit range. If it does not, the value overflows. - If the integer is negative, write it in 2's complement.
To decode, read the bits as an integer (unsigned or 2's complement) and divide by 2^f.
Here is a negative worked example. Encode -2.75 in 8 bits with f = 4.
- 2.75 × 16 = 44, which is
0010 1100. - Invert the bits:
1101 0011. Add 1:1101 0100, which is0xD4. - Check:
0xD4= 212, and 212 - 256 = -44. Then -44 / 16 = -2.75.
An 8-bit value cannot hold more than 4 fractional digits of precision. Encoding 3.14159 gives 50.265, which rounds to 50. Decoding gives 50 / 16 = 3.125. The error of 0.01659 is within the maximum rounding error of half a step, which is 1/32 = 0.03125.
Arithmetic
Addition and subtraction work on the raw integers, exactly as in 2's complement. Both operands must have the same f. Overflow is detected as before: adding two operands of the same sign gives a result of the opposite sign.
Multiplication changes the scale. If a and b are raw integers, then a × b has 2f fraction bits. To return to f bits you shift right by f. Shifting right discards low bits, so you must choose between truncation (floor) and rounding. To round, add half a step (2^(f-1)) before shifting.
Division goes the other way. Shift the numerator left by f first, then divide. Without that shift, a / b has no fraction bits at all.
The following program runs the examples above. It also compares truncation and rounding in multiplication, and compares wrapping and saturating addition.
W = 8
F = 4
SCALE = 1 << F
LO, HI = -(1 << (W - 1)), (1 << (W - 1)) - 1
def to_fixed(x):
n = round(x * SCALE)
if not LO <= n <= HI:
raise OverflowError(f"{x} does not fit (raw {n}, range {LO}..{HI})")
return n
def from_fixed(n):
return n / SCALE
def mul_trunc(a, b):
return (a * b) >> F
def mul_round(a, b):
return (a * b + (SCALE >> 1)) >> F
def div(a, b):
return (a << F) // b
def wrap_add(a, b):
return ((a + b - LO) & ((1 << W) - 1)) + LO
def sat_add(a, b):
return max(LO, min(HI, a + b))
a = to_fixed(0.3125)
print(a, mul_trunc(a, a), mul_round(a, a)) # 5 1 2
print(from_fixed(mul_trunc(a, a)), from_fixed(mul_round(a, a))) # 0.0625 0.125
x, y = to_fixed(2.75), to_fixed(1.5)
print(x, y, from_fixed(mul_trunc(x, y))) # 44 24 4.125
print(from_fixed(div(x, y))) # 1.8125
big = to_fixed(7.0)
print(from_fixed(wrap_add(big, big))) # -2.0
print(from_fixed(sat_add(big, big))) # 7.9375The exact product 0.3125 × 0.3125 is 0.09765625. Truncation gives 0.0625 and rounding gives 0.125. The rounded result is closer, but both lose information. The two additions show the difference between failure modes. Wrapping turns 14.0 into -2.0, which is a large silent error. Saturation clamps to the largest value, which is less harmful in signal-processing hardware.
Where the errors come from
Fixed-point has three separate error sources:
- Representation error. A value that is not a multiple of
2^-fmust be rounded. The worst case is2^-(f+1)with rounding to nearest, and2^-fwith truncation. - Operation error. Multiplication and division discard low bits after rescaling. Addition and subtraction of aligned operands are exact.
- Overflow. Results outside the range wrap or saturate, as the code above shows.
Note the key property of fixed-point error: the absolute error bound is the same for 0.1 and for 100. The relative error is therefore large for small values. Floating-point behaves the opposite way, and later chapters build on that contrast.
Choosing `f`
Because the word has w bits, every extra fraction bit costs one integer bit. A designer picks f from the largest magnitude needed and the finest step needed:
- Find the largest magnitude
Mthe variable will reach. The integer part needsceil(log2(M + 1))bits, plus a sign bit if signed. - Find the required step
d. The fraction needsf = ceil(log2(1 / d))bits. - Add the two to get
w. Ifwis fixed, the two requirements may conflict, and you must give up range or precision.
For example, a signed value up to 100 with a step of 0.01 needs 7 integer bits, because 2^7 = 128 > 100. It needs 7 fraction bits, because 2^-7 = 0.0078 ≤ 0.01. With a sign bit that gives 15 bits.
Common mistakes
- Applying the 2's complement range of a plain integer without dividing by
2^f. - Counting the sign bit as an integer bit when the question gives the integer bits separately. Check whether the stated total includes the sign.
- Forgetting to shift after multiplication, which leaves the result at scale
2^(2f). - Converting a negative fraction by negating the fractional bits alone. Convert the whole value to an integer first, then take the 2's complement of the whole word.
- Assuming that 0.1 is exact. It is exact in fixed-point only if it is a multiple of
2^-f. Withf = 4the nearest value to 0.1 is 2/16 = 0.125.
Exercise
A 10-bit signed 2's complement fixed-point format has 6 fraction bits. What are the smallest value, the largest value and the resolution?
Show answer
The integer range is -512 to 511, because w = 10. Dividing by 2^6 = 64 gives -8 to 511/64 = 7.984375. The resolution is 1/64 = 0.015625. The word has 3 integer bits plus the sign bit, so the range is -2^3 to 2^3 - 2^-6.
Exercise
In an 8-bit signed format with f = 4, decode the pattern 11100110. Then encode -1.625 and check that you get the same pattern.
Show answer
The pattern as an unsigned integer is 230, and 230 - 256 = -26. Then -26 / 16 = -1.625. To encode, 1.625 × 16 = 26 = 00011010. Inverting gives 11100101, and adding 1 gives 11100110, which matches.
Exercise
Using f = 4 with 8 bits, multiply 1.5 by 2.25 using raw integers. State the raw operands, the raw product, the shifted result, and the decoded value.
Show answer
Raw operands are 1.5 × 16 = 24 and 2.25 × 16 = 36. The raw product is 24 × 36 = 864, which has 8 fraction bits. Shifting right by 4 gives 54. The decoded value is 54 / 16 = 3.375, which equals the exact product 1.5 × 2.25. No bits were lost because both operands are multiples of 1/16 and the product is a multiple of 1/16.