Sub topic 6
Fixed-Point Representation
This chapter assumes that you can convert between decimal and binary, use powers of two, and perform basic binary addition. Fixed-point representation stores a real number as an integer while placing the binary point at a predetermined position. The hardware stores only the bits; the program remembers the scale.
The Basic Model
Suppose a format reserves \(F\) bits for the fractional part. The stored integer \(N\) represents
\[ x = \frac{N}{2^F} \]
Equivalently, the binary point is understood to lie \(F\) positions from the right. For example, with four fractional bits, the stored pattern 00110010 represents:
\[ 0011.0010_2 = 3 + \frac{2}{16} = 3.125 \]
The same pattern could also represent the integer 50 if no fixed-point interpretation were applied. The bit pattern alone does not identify the value. The format definition supplies the scale.
Encoding and Decoding
To encode a value \(x\), multiply it by \(2^F\) and round or truncate the result:
\[ N \approx x \times 2^F \]
To decode, divide \(N\) by \(2^F\).
Consider a format with eight total bits and four fractional bits. The value 3.125 is encoded as:
\[ 3.125 \times 16 = 50 = 00110010_2 \]
The smallest positive step is \(1/16\), so values such as 3.13 cannot be represented exactly. Rounding 3.13 to the nearest stored value gives \(3.125\).
FRAC_BITS = 4
SCALE = 1 << FRAC_BITS
def encode(value):
return round(value * SCALE)
def decode(stored):
return stored / SCALE
stored = encode(3.14159)
print(stored) # 50
print(format(stored, "08b")) # 00110010
print(decode(stored)) # 3.125Signed Fixed-Point Values
For signed values, the stored integer is usually an \(n\)-bit two's complement integer. The decoding rule remains:
\[ x = \frac{N}{2^F} \]
For eight bits with four fractional bits, the signed integer range is \(-128\) through \(127\). Therefore, the fixed-point range is:
\[ -128 \leq x \leq 127.9375 \]
The resolution, or distance between adjacent values, is \(2^{-4}=0.0625\).
| Format | Integer bits including sign | Fraction bits | Resolution |
|---|---|---|---|
| 8-bit signed fixed point | 4 | 4 | \(1/16\) |
| 16-bit signed fixed point | 8 | 8 | \(1/256\) |
Some specifications write this format as Q4.4, meaning four integer-side bits and four fractional bits. Others count the sign bit differently, so always confirm the convention before calculating the range.
Worked Arithmetic
Let \(F=4\), and encode \(1.5\) and \(2.25\):
- \(1.5 \times 16 = 24\)
- \(2.25 \times 16 = 36\)
Adding stored integers gives \(24+36=60\). Decoding gives \(60/16=3.75\), which is the exact mathematical sum.
Multiplication needs rescaling:
\[ \frac{24 \times 36}{16} = 54 \]
The decoded result is \(54/16=3.375\).
Exercise
An unsigned 8-bit fixed-point format has four fractional bits. Encode 5.625 and state the resolution.
Show answer
Multiply by \(2^4=16\): \(5.625 \times 16=90\), so the bit pattern is 01011010. The resolution is \(1/16=0.0625\).