C7 · Lesson 17 · Arithmetic circuits
Subtraction, overflow, and adding faster than the carry can ripple
A ripple-carry adder is the obvious way to add: full adders in a row, each carry feeding the next. Its weakness is that the last sum bit can't settle until a carry has crawled through every stage, so a 64-bit adder would be 64 carries slow. Carry lookahead computes every carry directly from the inputs, in a few gate delays whatever the width. The same adder subtracts, with an inversion and a carry-in, and two extra gates tell you when the answer doesn't fit.
- Two ways to compute the carry out of a 4-bit addition, both adding A = 1111 and B: R4 by rippling through four stages, L4 by carry lookahead.
- At tick 10, rises from 0 to 1, so 1111 + 0001 overflows into the carry: the change must pass through every bit.
- Compare the waveforms: L4 rises a few gate delays after , R4 several delays later. Each extra bit adds to the ripple, but not to the lookahead.
Predict
An 8-bit two's-complement adder adds 100 and 50. What does it produce, and is it correct?
−106: an overflow. The largest 8-bit two's-complement value is 127, so 150 doesn't fit; the bit pattern 10010110 is read as . Adding two positives and getting a negative is the sign of overflow, and the adder's overflow flag catches it.
Name it
Subtraction
In two's complement, , so : invert B with XOR gates (each ) and set the carry-in to 1. One adder does both.
Overflow and flags
Adding two n-bit two's-complement numbers overflows when both have the same sign and the sum's sign differs:
For unsigned numbers the warning is instead the carry out . A processor's ALU sets four flags after each operation: zero (Z), negative (N), carry (C) and overflow (V); comparisons are subtractions that keep only the flags.
Carry lookahead
For bit i, generate (this bit makes a carry by itself) and propagate (it passes an incoming carry on). Then , which expands to
two levels of logic after the G and P gates, whatever the bit position. Wide gates get slow, so real adders compute group G and P for blocks of 4 bits and look ahead between blocks, or use prefix adders whose delay grows as . A ripple adder's delay grows as n.
Worked example
Compute 23 − 45 with an 8-bit adder.
Show the solution
- Invert 45. 45 = 00101101, which inverted is 11010010.
- Add 23 and a carry-in of 1. With 23 = 00010111:
- Read the result. 11101010 is 234 unsigned, so as a signed number it's
- Read the carry. The carry out is 0, which for unsigned subtraction means a borrow: 23 < 45.
Answer 11101010, which is −22, with a carry out of 0 (a borrow).
Worked example
A ripple adder's carry takes 2 gate delays per bit. A lookahead adder computes each carry in 3 (one for G and P, two for the sum of products). Compare 4 and 16 bits.
Show the solution
- Ripple. At 2 delays per bit, it takes 8 delays at 4 bits and 32 at 16.
- Single-level lookahead. It takes 3 delays at any width, but a 16-bit lookahead needs 17-input gates.
- Two-level lookahead. Split into four 4-bit blocks with a second lookahead level, the 16-bit adder takes about 5 delays.
Answer at 4 bits, 8 delays for ripple against 3 for lookahead; at 16 bits, 32 against about 5 with two lookahead levels.
Worked example
Add −100 and −50 as 8-bit two's-complement numbers. What's the result, and how does the overflow formula detect the error?
Show the solution
- Encode the operands. −100 = 0x9C and −50 = 0xCE.
- Add. The low eight bits are 0x6A = 106, with a carry out of the top, = 1.
- Find the carry into bit 7. The low seven bits produce no carry: so = 0.
- Overflow flag. overflow: the true answer, −150, is below −128.
- The sign rule. Two negatives gave a positive, the same sign rule seen from the carries.
Answer the 8-bit result is 0x6A = 106, wrong, and flags the overflow.
Check
Compute 23 − 70 in 8-bit two's complement. Give the result as 8 bits.
Check
A, B and S are the sign bits of two addends and their sum. Write the overflow flag.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
Write the lookahead carry from , , , and .
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A ripple-carry adder's carry takes 1 gate delays per bit. What is the worst-case carry delay for 16 bits?
Check
A prefix adder combines generate and propagate in a tree. How many levels does its carry network need for 16 bits?
Check
An 8-bit adder adds 200 and 100 as unsigned numbers. Which flag shows the result is wrong?
Check
−100 and −60 are added as 8-bit two's-complement numbers.
(a)
What signed value does the 8-bit result read as?
(b)
What's the overflow flag ?
Check
An n-bit adder is built from gates of delay t.
Step 1
What's a ripple-carry adder's worst delay, at 2 gate delays per bit?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
A carry-select adder ripples within blocks of k bits (2t per bit) and then passes through one 2t multiplexer per block. What's its delay?
Step 3
What block size minimizes that delay?
Explain why
How does carry lookahead avoid waiting for the carry to ripple?
A ripple adder computes each carry from the one before, so the last carry has to wait for every earlier stage in turn. But each carry is just a function of the input bits and the carry-in, and it can be written out directly: a carry leaves bit i if some lower bit generated one and every bit in between propagates it. Computing the generate and propagate signals takes one gate delay for all bits at once, and the expanded carry equations are a two-level sum of products, so every carry is ready after a fixed few gate delays, however far along the word it is.
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.