Division is long division in binary
Remember long division on paper: bring down the next digit, see how many times the divisor fits, write that digit of the quotient, subtract, repeat. In binary the question is easier: the divisor fits either once (1) or not at all (0).
A CPU’s divider uses three registers:
- Q starts with the dividend and slowly fills with the quotient bits.
- A starts at 0 and ends with the remainder. It has one extra bit, the sign bit.
- M holds the divisor.
Each round shifts A:Q one place left, which brings the next dividend bit into A, exactly like “bringing down” a digit.
Restoring division
A ← 0, Q ← dividend, M ← divisor
repeat n times:
shift A:Q left
A ← A − M
if A < 0: Q₀ ← 0; A ← A + M (restore)
else: Q₀ ← 1
quotient = Q, remainder = A
Example: 11 ÷ 3 (Q = 1011, M = 00011):
| Round | After shift (A, Q) | A − M | Q₀ | A at the end of the round |
|---|---|---|---|---|
| 1 | 00001, 011_ | −2 (negative) | 0 | restored to 00001 |
| 2 | 00010, 110_ | −1 (negative) | 0 | restored to 00010 |
| 3 | 00101, 100_ | 2 | 1 | 00010 |
| 4 | 00101, 001_ | 2 | 1 | 00010 |
Result: Q = 0011 = 3, A = 00010 = 2. Indeed 3 × 3 + 2 = 11.
Non-restoring division
Restoring wastes work: when A goes negative we add M back, and in the next round we shift and subtract M again. Since restoring then shifting then subtracting gives the same A as shifting the negative value and adding M, we can skip the restore:
A ← 0, Q ← dividend, M ← divisor
repeat n times:
shift A:Q left
if A (before the shift) ≥ 0: A ← A − M
else: A ← A + M
Q₀ ← 1 if A ≥ 0 else 0
if A < 0: A ← A + M (final correction)
For 11 ÷ 3 the rounds give A = −2, −1, 2, 2 and the same quotient 0011, using only 4 add/subtract operations instead of 6.
| Restoring | Non-restoring | |
|---|---|---|
| Operations per round | 1 or 2 | exactly 1 |
| Extra step at the end | none | one correction if A < 0 |
| Hardware | simpler control | faster, used in real dividers |
Code
def restoring_divide(q, m, n=4):
A, Q = 0, q
for _ in range(n):
A = (A << 1) | ((Q >> (n - 1)) & 1) # shift A:Q left
Q = (Q << 1) & ((1 << n) - 1)
A -= m
if A < 0:
A += m # restore, Q0 stays 0
else:
Q |= 1 # Q0 = 1
return Q, A
def nonrestoring_divide(q, m, n=4):
A, Q = 0, q
for _ in range(n):
was_negative = A < 0
A = A * 2 + ((Q >> (n - 1)) & 1)
Q = (Q << 1) & ((1 << n) - 1)
A = A + m if was_negative else A - m
if A >= 0:
Q |= 1
if A < 0:
A += m # final correction
return Q, A
print(restoring_divide(11, 3), nonrestoring_divide(11, 3)) # (3, 2) (3, 2)
Where is it used?
Integer division instructions (DIV on x86, UDIV on ARM) use refinements of these ideas. Modern CPUs use SRT division, a faster relative of non-restoring division that produces several quotient bits per cycle. Even so, division is still one of the slowest instructions, often 10 to 40 times slower than an addition. That is why compilers replace division by a constant with a multiplication whenever they can.
Common mistakes
- Forgetting the extra sign bit in A. Without it you can’t tell that A − M went negative.
- In restoring division, setting Q₀ = 1 after a negative result. Negative means “doesn’t fit”, so Q₀ = 0.
- In non-restoring division, deciding add or subtract from the new A instead of the sign of A before the shift.
- Skipping the final correction in non-restoring division when A ends up negative.
Complexity at a glance
| Case / operation | Time | Why |
|---|---|---|
| Rounds (n-bit dividend) | n | One quotient bit per round. |
| Restoring — add/subtract operations | up to 2n | A subtract every round, plus an add when restoring. |
| Non-restoring — add/subtract operations | n (+ 1) | One per round, plus at most one final correction. |
| Extra space | Registers A (n + 1 bits), Q (n bits), M (n bits) |
Quick check
Test yourself — pick an answer to see if you got it.
1. In restoring division, what happens when A becomes negative after A ← A − M?
A negative result means the divisor did not fit, so the quotient bit is 0 and the subtraction is undone.
2. What is the main advantage of non-restoring division?
Restoring gets back A and next round computes 2A − M. Non-restoring keeps A − M and next round computes 2(A − M) + M = 2A − M: the same value, one operation fewer.
3. How many bits does register A need when dividing n-bit numbers?
The extra bit is the sign bit, which shows whether A − M went negative.
4. Using 4-bit division, what are the quotient and remainder of 11 ÷ 3?
3 × 3 + 2 = 11. The algorithm ends with Q = 0011 and A = 00010.