C7 · Lesson 22 · Number formats lab
Two's complement, flags, floats, Gray code and fixed point, in Python
Every number a digital circuit handles is a pattern of bits with an agreed meaning, and most bugs at the hardware-software boundary are disagreements about that meaning: a value read as unsigned that was meant as signed, an overflow nobody checked, a float compared for exact equality, a fixed-point product that wrapped instead of saturating. In this lab you'll write the conversions and the flag logic of a processor's adder, take floats apart into their fields, and build fixed-point arithmetic that saturates.
Play
- The staircase is the spacing between neighboring single-precision floats, against the value itself, both on log scales.
- The spacing doubles at every power of two: floats keep about the same relative precision, about 1 part in 8 million, everywhere.
- Compare with fixed point, whose spacing is the same for every value: fine for large numbers, coarse relative to small ones.
Predict
A program adds 0.1 to a float ten times and compares the result with 1.0. Is it equal?
Not necessarily. 0.1 in binary is a repeating fraction, 0.000110011..., so the stored value is slightly off, and each addition rounds again. In single precision the sum comes out as 1.0000001 instead of 1. Floats should be compared with a tolerance, not for equality.
Name it
Two's complement and flags
An n-bit pattern means in two's complement. An adder sets four flags:
- C (carry): the carry out of the top bit, an unsigned overflow;
- V (overflow): the operands have the same sign and the result the other, a signed overflow, ;
- Z (zero) and N (negative, the top bit).
IEEE 754 single precision
1 sign bit, 8 exponent bits with a bias of 127, and 23 fraction bits with a hidden leading 1: . Python's struct module packs a float into its 4 bytes and back.
Fixed point
A Q-format value with q fraction bits stores as an integer. A product of two Q15 numbers has 30 fraction bits; shifting right by 15 (with rounding) returns it to Q15. Results outside the range should saturate at the largest or smallest value instead of wrapping, which turns an overflow into a small error instead of a sign flip.
Worked example
Add 0x70 and 0x30 in an 8-bit adder. What are the result and the four flags?
Show the solution
- Add.
- Carry. No carry out of bit 7, so C = 0.
- Overflow. Both operands are positive but the result's top bit is 1, so V = 1: as signed numbers, 112 + 48 gave −96.
- Zero and negative. Z = 0 and N = 1.
- Read it both ways. An unsigned program sees 160, correct; a signed one sees an overflow it must handle.
- What the flags are for. The adder computes one pattern; the flags let either reading check it.
Answer 0xA0 (160), with C = 0, V = 1, Z = 0 and N = 1.
Worked example
Pack 6.5 and −12.5 as single-precision floats and read back their fields.
Show the solution
- Pack 6.5. so s = 0, e = 129, f = 0.625: 0x40D00000.
- Pack −12.5. so s = 1, e = 130, f = 0.5625: 0xC1480000.
- Unpack. Unpacking 0xC1480000 gives back exactly −12.5, since both values have short binary fractions.
- A value that doesn't fit. A value like 0.1 doesn't, and unpacking its pattern gives 0.100000001490116.
Answer 6.5 is 0x40D00000 (s = 0, e = 129, f = 0.625) and −12.5 is 0xC1480000 (s = 1, e = 130, f = 0.5625); both read back exactly.
Worked example
Multiply 0.75 by 0.5 in Q15, then multiply −1 by −1.
Show the solution
- Convert to Q15. 0.75 is 24576 and 0.5 is 16384.
- Multiply. with 30 fraction bits.
- Round and shift. Adding half a step and shifting right by 15 gives 12288, which is 0.375.
- Square −1. In Q15, −1 is −32768, and its square, shifted, would be +32768: one more than the largest Q15 value, 32767.
- Wrap or saturate. Wrapping would give −1, the wrong sign; saturating gives 32767, about 0.99997.
- In hardware. DSP processors saturate in hardware for exactly this case.
Answer 0.75 × 0.5 gives 12288, which is 0.375; −1 × −1 gives +32768, which saturates to 32767 (about 0.99997).
Check
Step 1. Write to_signed(x, n) returning the value of the n-bit pattern x read as two's complement, and to_bits(v, n) returning the n-bit pattern (as a non-negative integer) for the value v.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write add_flags(a, b, n) for n-bit patterns a and b, returning (result, C, V, Z, N).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write float_fields(x) returning (s, e, f): the sign bit, the biased exponent and the fraction (as a number between 0 and 1) of x stored as an IEEE single, and float_bits(x) returning its 32-bit pattern as an integer.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 4. Write gray(b) and from_gray(g) converting between binary and Gray code for non-negative integers.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 5. Write q15_mul(a, b) multiplying two Q15 integers (−32768 to 32767) with round-to-nearest and saturation.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The 8-bit patterns 100 and 40 (both below 128) are added.
(a)
What's the overflow flag V?
(b)
What's the result read as signed?
Check
What's the spacing between neighboring single-precision floats with biased exponent 127?
Explain why
Why do signal-processing processors saturate instead of wrapping on overflow?
When a fixed-point result exceeds the largest representable value, wrapping keeps only the low bits, which turns a large positive number into a large negative one: the sign flips and the error is nearly the whole range. In an audio or control signal that's a loud click or a violent jump. Saturating clamps the result to the largest value of the right sign, so the error is only the part that didn't fit, and the signal is clipped rather than inverted. A small error of the right sign is almost always better than a huge error of the wrong one.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.