C722 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

Two's complement and flags

An n-bit pattern means −2n−1bn−1+∑i<n−12ibi 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, Cn⊕Cn−1;
  • 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: (−1)s×1.f×2e−127. Python's struct module packs a float into its 4 bytes and back.

Fixed point

A Q-format value with q fraction bits stores x×2q 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
  1. Add. 112+48=160=0xA0
  2. Carry. No carry out of bit 7, so C = 0.
  3. Overflow. Both operands are positive but the result's top bit is 1, so V = 1: as signed numbers, 112 + 48 gave −96.
  4. Zero and negative. Z = 0 and N = 1.
  5. Read it both ways. An unsigned program sees 160, correct; a signed one sees an overflow it must handle.
  6. 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
  1. Pack 6.5. 6.5=1.1012×22 so s = 0, e = 129, f = 0.625: 0x40D00000.
  2. Pack −12.5. −12.5=−1.5625×23 so s = 1, e = 130, f = 0.5625: 0xC1480000.
  3. Unpack. Unpacking 0xC1480000 gives back exactly −12.5, since both values have short binary fractions.
  4. 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
  1. Convert to Q15. 0.75 is 24576 and 0.5 is 16384.
  2. Multiply. 24576×16384=402653184 with 30 fraction bits.
  3. Round and shift. Adding half a step and shifting right by 15 gives 12288, which is 0.375.
  4. Square −1. In Q15, −1 is −32768, and its square, shifted, would be +32768: one more than the largest Q15 value, 32767.
  5. Wrap or saturate. Wrapping would give −1, the wrong sign; saturating gives 32767, about 0.99997.
  6. 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?

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.