C717 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, B0 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 B0, 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?

Name it

Subtraction

In two's complement, −B=B¯+1, so A−B=A+B¯+1: invert B with XOR gates (each Bi⊕SUB) 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:

V=An−1Bn−1S¯n−1+A¯n−1B¯n−1Sn−1=Cn⊕Cn−1

For unsigned numbers the warning is instead the carry out Cn. 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 Gi=AiBi (this bit makes a carry by itself) and propagate Pi=Ai⊕Bi (it passes an incoming carry on). Then Ci+1=Gi+PiCi, which expands to

C2=G1+P1G0+P1P0C0C4=G3+P3G2+P3P2G1+P3P2P1G0+P3P2P1P0C0

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 log2⁡n. A ripple adder's delay grows as n.

Worked example

Compute 23 − 45 with an 8-bit adder.

Show the solution
  1. Invert 45. 45 = 00101101, which inverted is 11010010.
  2. Add 23 and a carry-in of 1. With 23 = 00010111: 00010111+11010010+1=11101010
  3. Read the result. 11101010 is 234 unsigned, so as a signed number it's 234−256=−22
  4. 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
  1. Ripple. At 2 delays per bit, it takes 8 delays at 4 bits and 32 at 16.
  2. Single-level lookahead. It takes 3 delays at any width, but a 16-bit lookahead needs 17-input gates.
  3. 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 V=C8⊕C7 detect the error?

Show the solution
  1. Encode the operands. −100 = 0x9C and −50 = 0xCE.
  2. Add. 0x9C+0xCE=0x16A The low eight bits are 0x6A = 106, with a carry out of the top, C8 = 1.
  3. Find the carry into bit 7. The low seven bits produce no carry: 0x1C+0x4E=0x6A so C7 = 0.
  4. Overflow flag. V=C8⊕C7=1⊕0=1 overflow: the true answer, −150, is below −128.
  5. 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 V=1 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 C2 from G1, P1, G0, P0 and C0.

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 C8⊕C7?

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°.

Explain why

How does carry lookahead avoid waiting for the carry to ripple?

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.