C7 · Lesson 53 · Arithmetic lab
Lookahead carries, Booth multiplication, division, BCD and saturation, in Python
Lessons 16 to 20 built adders, multipliers and number formats as circuits. Each algorithm a circuit implements can be written first as a few lines of code that work bit by bit, exactly as the hardware does, and checked against ordinary arithmetic on every input. In this lab you'll compute carries by lookahead, multiply signed numbers with Booth's algorithm, divide by restoring subtraction, add decimal digits in BCD, and add with saturation, testing each exhaustively over small widths, which is how hardware arithmetic is verified.
Play
- The curve shows how many add or subtract operations Booth's algorithm needs, on average, against the number of bits, compared with plain shift-and-add, which adds once per 1 bit in the multiplier.
- Booth adds only at the boundaries of runs of 1s. For random multipliers that's about half the bits either way; for multipliers with long runs of 1s, far fewer.
- The readout gives the worst case for each: Booth's is a multiplier of alternating 1s and 0s.
Predict
Booth's algorithm multiplies by 0111 1110 (126). How many additions or subtractions does it do, against shift-and-add's one per 1 bit?
Two. Booth rewrites a run of 1s from bit i to bit j as : here = 126, one subtraction of the multiplicand shifted by 1 and one addition shifted by 7. Shift-and-add would add six times. Booth handles signed numbers without special cases, which is its main use in hardware.
Name it
Lookahead carries
With and , every carry can be written directly from the inputs:
Computing each carry this way, with no carry waiting on another, is what a lookahead adder does in parallel.
Booth multiplication
Scan the multiplier's bits from the bottom, each with the bit below it (a 0 below the lowest). At a pair 10 (a run of 1s starts), subtract the multiplicand; at 01 (a run ends), add it; otherwise do nothing. Shift the accumulator and multiplier right together, arithmetically, after each bit. After n steps the 2n-bit result is the signed product.
Restoring division
For each dividend bit from the top: shift it into a partial remainder; if the remainder is at least the divisor, subtract and set that quotient bit to 1. After n steps, the quotient and remainder are complete.
BCD addition and saturation
Add BCD digit by digit; any digit sum above 9 gets 6 added and carries into the next digit. A saturating n-bit adder clamps results to and instead of wrapping.
Worked example
Multiply −7 by 3 with 4-bit Booth's algorithm.
Show the solution
- Write the multiplier. The multiplier 3 is 0011.
- Bit 0. With a 0 below it's 10: subtract −7, so the accumulator gains 7, then shift.
- Bit 1. With 1 below it's 11: nothing.
- Bit 2. With 1 below it's 01: add −7.
- Bit 3. With 0 below: nothing.
- Read the result. Two operations, and the 8-bit result reads −21.
- Why it works. Booth treated 0011 as :
Answer −21, from two operations.
Worked example
Divide 143 by 11 by restoring division with 8 bits.
Show the solution
- Shift in until it fits. Shifting in 143 = 1000 1111 bit by bit, the partial remainder first reaches 11 or more at the fifth bit: 1000 1 = 17, subtract 11 → 6.
- Shift in the rest. 6 → 13 (subtract → 2), 2 → 5, 5 → 11 (subtract → 0).
- Read the result. The quotient bits form 0000 1101 = 13 and the remainder is 0:
- The cost. Each step is a shift and a compare-subtract, so an n-bit divider takes n steps, far more than an adder.
Answer the quotient is 13 and the remainder 0.
Worked example
Add the BCD numbers 59 and 27, and 999 and 1.
Show the solution
- Units. 9 + 7 = 16, above 9, so add 6: digit 6, carry 1.
- Tens. so the result is 86.
- 999 + 1. Each digit overflows in turn: 10 → 0 carry 1, three times, giving 1000.
- In hardware. The correction ripples like a carry, and BCD hardware is a binary adder with a correction stage per digit.
Answer 59 + 27 = 86, and 999 + 1 = 1000.
Check
Step 1. Write cla_carries(a, b, cin, n) returning the list of carries (= cin) to , each computed directly from the g and p terms by the lookahead formula, not from the carry before it.
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 booth(a, b, n) returning (product, ops): the signed product of the n-bit two's-complement numbers a and b by radix-2 Booth, and the number of additions plus subtractions it performed.
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 restoring_divide(N, D, n) returning (quotient, remainder) for unsigned n-bit N and D > 0.
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 bcd_add(x, y) adding two BCD numbers (each a Python integer whose hex digits are the decimal digits, like 0x59 for 59) and returning the BCD sum.
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 saturating_add(a, b, n) returning the n-bit signed saturating sum of a and b.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A multiplier's only 1s are a single run of 6 consecutive bits starting at bit 3. How many additions or subtractions does Booth's algorithm perform?
Explain why
Why are hardware arithmetic algorithms usually verified by exhaustive testing at small widths?
An arithmetic algorithm implemented bit by bit has many corner cases: carries that ripple through every bit, the most negative two's-complement number, remainders exactly equal to the divisor, digits that overflow in BCD. A few hand-picked tests easily miss one. At a small width the number of input combinations is small enough to try them all and compare each with ordinary arithmetic, so every corner case is covered. Because the same algorithm scales by repeating the same bit-level steps, an algorithm correct for every 5-bit or 6-bit input is very likely correct at 32 or 64 bits, where exhaustive testing would be impossible.
Practice
Keep practicing
2 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.