C7 · Lesson 21 · Division in hardware
Long division one bit at a time, dividing by constants without dividing, and the trouble with negative numbers
Lesson 20 built multipliers that finish in a few gate delays. Division is harder: each quotient bit depends on whether the previous subtraction went negative, so the work is inherently sequential, and a 32-bit divide takes tens of clock cycles even in fast processors. Hardware designers have three answers: do it one bit per cycle with a small circuit, avoid it by multiplying with a precomputed reciprocal when the divisor is a constant, and shift when it's a power of two. The last is where a classic bug lives: shifting a negative number right doesn't round the way division does.
- Long division in binary, one bit per step. The remainder doubles and takes in the next dividend bit; if it's at least the divisor, the divisor is subtracted and the quotient bit is 1.
- Drag the dividend and divisor and step through. The number of steps is the number of dividend bits, whatever the values.
- The curve is the remainder after each step for the chosen values, which always stays below the divisor.
Predict
A C program computes −7 / 2 with integers. A compiler that "optimizes" it into an arithmetic right shift by one bit would get what?
−4. In two's complement, −7 is 1001; shifting right arithmetically gives 1100, which is −4: the shift rounds down, toward minus infinity. C's division truncates toward zero, giving −3 with a remainder of −1. Compilers fix this by adding 1 (in general ) to negative dividends before shifting by .
Name it
Restoring division
To divide an -bit by , keep a remainder , starting at 0. For each bit of from the top:
- (next bit of )
- If : and the quotient bit is 1; otherwise it's 0.
After steps the quotient bits form and . The hardware is a subtractor, a comparator (the subtractor's borrow out), a shift register and a counter: one quotient bit per cycle. It's called restoring because the simplest form subtracts first and adds back when the result went negative.
Non-restoring division skips the add-back: after a negative result it adds on the next step instead of subtracting, which saves time per step. Fast processors use SRT division, which produces two or more quotient bits per cycle from a small lookup table, and even then a 64-bit divide costs tens of cycles, ten or more times a multiply.
Dividing by a constant
If is known in advance, multiply by a scaled reciprocal and shift:
which is exact for every -bit when is large enough: roughly . For = 10 and 16-bit , = 52 429 and = 19. Compilers do this automatically for division by constants; it turns a 30-cycle divide into a 3-cycle multiply.
Powers of two and negative numbers
Dividing by is a right shift. For unsigned numbers it's exact. For signed numbers, an arithmetic shift rounds toward −∞, but C, Java and most hardware divide instructions truncate toward zero, with a remainder that takes the dividend's sign: remainder . Adding to a negative dividend before the shift gives the truncated result.
Worked example
Divide 13 (1101) by 3 (0011) with the restoring algorithm.
Show the solution
- Shift in one bit per step. Each step doubles the partial remainder R, adds the next bit of 1101, and subtracts 3 when it can:
| Step | + bit | ? | after | Quotient bit |
|---|---|---|---|---|
| 1 | 0 + 1 = 1 | no | 1 | 0 |
| 2 | 2 + 1 = 3 | yes | 0 | 1 |
| 3 | 0 + 0 = 0 | no | 0 | 0 |
| 4 | 0 + 1 = 1 | no | 1 | 0 |
- Read the result. The quotient is 0100 = 4 and the remainder 1:
- Count the steps. Four steps for four bits, regardless of the values.
Answer the quotient is 4 (0100) and the remainder 1.
Worked example
Show that for 16-bit , by bounding the error.
Show the solution
- Write the multiplier as a fraction. so with .
- Bound the excess. For , the excess is under .
- Compare with the fractions. The fractional part of is a multiple of 0.1, at most 0.9, so adding less than 0.025 can never push it past the next integer: the floors agree.
- Check. A program that checks all 65 536 values confirms it.
- Why it's useful. On a microcontroller without a divide instruction, that's one 16 × 16 multiply and a shift instead of a slow software loop.
Answer the error stays under 0.025, too small to carry a fraction of at most 0.9 past the next integer, so the two floors agree for every 16-bit .
Worked example
A 32-bit restoring divider in a 500 MHz design takes one cycle per quotient bit plus two cycles of setup. How long is a divide, and how does that compare with a multiplier that finishes in 3 cycles? What would radix-4 SRT do?
Show the solution
- Divide time. cycles at 2 ns:
- Compare with the multiplier. Its 3 cycles take 6 ns, so the divide is more than ten times slower.
- Radix-4 SRT. It produces two quotient bits per cycle, so about cycles, 36 ns.
- What compilers do. That's why compilers replace division by constants with multiplication, and why code in tight loops is written to multiply by reciprocals.
Answer 68 ns, more than ten times the multiplier's 6 ns; radix-4 SRT would take about 36 ns.
Check
A restoring divider computes 104 ÷ 4. What quotient does it produce?
Check
What remainder does the divider leave for 125 ÷ 7?
Check
A restoring divider produces one quotient bit per step. How many steps does a 16-bit by 16-bit division take, not counting setup?
Check
In C, what is −13 / 2 for signed integers?
Check
A compiler replaces y = x / 8 with y = x >> 3 for a signed int x. When does the result differ from C's division?
Check
Write divide(N, Dv) returning (q, r) by the restoring algorithm for n-bit unsigned N (12 bits), counting the steps in a global list steps (append 1 per quotient bit). Then find the smallest shift s (search from n upward) such that M = ceil(2**s / D) gives (x*M) >> s == x // D for every n-bit x, with D = 3, and set s_min and M_min.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is division so much slower than multiplication in hardware, even though both are built from adders?
A multiplier forms all its partial products at once, since each is just the multiplicand ANDed with one multiplier bit, and then adds them in a tree of adders whose depth grows only with the logarithm of the number of bits. Division can't do that: whether the next quotient bit is 1 depends on the sign of the remainder after the previous subtraction, so each step must wait for the previous one to finish, all the way down a carry chain. The work is a chain of n dependent subtractions instead of a tree, so it takes about n steps, or n/2 for radix-4 SRT, against a few for multiplication. That's why compilers turn division by constants into multiplication by reciprocals.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.