1. Home
  2. Computer Organization & Architecture
  3. Restoring & Non-Restoring Division

Restoring & Non-Restoring Division

Binary long division in hardware. Shift, try to subtract the divisor, and write 1 if it fits or 0 if not. Non-restoring skips the undo step.

Interactive 3DIntermediate12 min readCOAUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

How does a CPU divide? Just like long division on paper, but in binary: shift, try to subtract the divisor, and write a 1 if it fits or a 0 if it doesn’t. Choose a method and press Divide.

Pseudocode

  1. A ← 0; Q ← dividend; M ← divisor; count ← n
  2. repeat n times:
  3. shift A:Q left by one bit
  4. A ← A − M
  5. if A < 0: Q₀ ← 0; A ← A + M // restore
  6. else: Q₀ ← 1
  7. quotient = Q; remainder = A
Read this model as text · 17 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. How does a CPU divide? Just like long division on paper, but in binary: shift, try to subtract the divisor, and write a 1 if it fits or a 0 if it doesn’t. Choose a method and press Divide.
  2. 11 ÷ 3 with 4-bit numbers. Start: A = 0 (it will hold the remainder), Q = 1011 (the dividend, becomes the quotient), M = 00011. We need 4 rounds — one per quotient bit.
  3. Round 1: shift A:Q left. The top bit of Q (1) moves into A; Q₀ is empty for now. Shifting brings down the next dividend bit — like long division.
  4. A ← A − M (add −M) = -2. The sign bit is 1: A is negative, M did not fit.
  5. A < 0 → the quotient bit is Q₀ = 0.
  6. Restore: A ← A + M = 1, undoing the subtraction. (Non-restoring division skips this step and fixes it in the next round instead.)
  7. Round 2: shift A:Q left. The top bit of Q (0) moves into A; Q₀ is empty for now. Shifting brings down the next dividend bit — like long division.
  8. A ← A − M (add −M) = -1. The sign bit is 1: A is negative, M did not fit.
  9. A < 0 → the quotient bit is Q₀ = 0.
  10. Restore: A ← A + M = 2, undoing the subtraction. (Non-restoring division skips this step and fixes it in the next round instead.)
  11. Round 3: shift A:Q left. The top bit of Q (1) moves into A; Q₀ is empty for now. Shifting brings down the next dividend bit — like long division.
  12. A ← A − M (add −M) = 2. The sign bit is 0: M fits.
  13. A ≥ 0 → the quotient bit is Q₀ = 1.
  14. Round 4: shift A:Q left. The top bit of Q (1) moves into A; Q₀ is empty for now. Shifting brings down the next dividend bit — like long division.
  15. A ← A − M (add −M) = 2. The sign bit is 0: M fits.
  16. A ≥ 0 → the quotient bit is Q₀ = 1.
  17. Done: quotient Q = 0011 = 3, remainder A = 2. Check: 3 × 3 + 2 = 11 ✓. Restoring division needed an extra add every time A went negative.

Try this in the 3D model

  • Divide 11 ÷ 3 with the restoring method. In which rounds does A go negative?
  • Switch to Non-restoring and divide 11 ÷ 3 again. Count the add/subtract operations in each method.
  • Try 7 ÷ 3 and check the remainder against ordinary arithmetic.
  • Try a divisor bigger than the dividend, for example 5 ÷ 9. What quotient do you get?

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 / operationTimeWhy
Rounds (n-bit dividend)nOne quotient bit per round.
Restoring — add/subtract operationsup to 2nA subtract every round, plus an add when restoring.
Non-restoring — add/subtract operationsn (+ 1)One per round, plus at most one final correction.
Extra spaceRegisters 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?

2. What is the main advantage of non-restoring division?

3. How many bits does register A need when dividing n-bit numbers?

4. Using 4-bit division, what are the quotient and remainder of 11 ÷ 3?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Restoring & Non-Restoring Division. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.