---
title: Arithmetic circuits
heading: Subtraction, overflow, and adding faster than the carry can ripple
eyebrow: C7 · Lesson 17 · Arithmetic circuits
xp: 40
---

::: hook
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.
:::

::: play
- 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, $B_0$ 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 $B_0$, R4 several delays later. Each extra bit adds to the ripple, but not to the lookahead.
:::
:::

```sim logic
input A0, A1, A2, A3, B0, B1, B2, B3
output R4, L4
set A0 = 1
set A1 = 1
set A2 = 1
set A3 = 1
stim B0 0@0 1@10
assign c1 = A0 & B0
assign c2 = A1 & B1 | (A1 ^ B1) & c1
assign c3 = A2 & B2 | (A2 ^ B2) & c2
assign R4 = A3 & B3 | (A3 ^ B3) & c3
assign L4 = A3 & B3 | (A3 ^ B3) & A2 & B2 | (A3 ^ B3) & (A2 ^ B2) & A1 & B1 | (A3 ^ B3) & (A2 ^ B2) & (A1 ^ B1) & A0 & B0
wave: B0, c1, c2, c3, R4, L4
time: 30
```

::: predict
## An 8-bit two's-complement adder adds 100 and 50. What does it produce, and is it correct?

- [ ] 150, correct
- [x] −106, an overflow
- [ ] 50, the carry is lost
- [ ] 0

::: reveal
−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 $150 - 256 = -106$. Adding two positives and getting a negative is the sign of overflow, and the adder's overflow flag catches it.
:::
:::

::: nameit
## Subtraction

In two's complement, $-B = \bar B + 1$, so $A - B = A + \bar B + 1$: invert B with XOR gates (each $B_i \oplus \text{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 = A_{n-1}B_{n-1}\bar S_{n-1} + \bar A_{n-1}\bar B_{n-1}S_{n-1} = C_n \oplus C_{n-1}$$

For **unsigned** numbers the warning is instead the carry out $C_n$. 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** $G_i = A_iB_i$ (this bit makes a carry by itself) and **propagate** $P_i = A_i \oplus B_i$ (it passes an incoming carry on). Then $C_{i+1} = G_i + P_iC_i$, which expands to

$$C_2 = G_1 + P_1G_0 + P_1P_0C_0 \qquad C_4 = G_3 + P_3G_2 + P_3P_2G_1 + P_3P_2P_1G_0 + P_3P_2P_1P_0C_0$$

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 $\log_2 n$. A ripple adder's delay grows as n.
:::

::: example
## Compute 23 − 45 with an 8-bit adder.

::: solution
1. **Invert 45.** 45 = 00101101, which inverted is 11010010.
2. **Add 23 and a carry-in of 1.** With 23 = 00010111:
   $$\begin{aligned} &00010111 + 11010010 + 1 \\ &= 11101010 \end{aligned}$$
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).
:::
:::

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

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

::: example
## Add −100 and −50 as 8-bit two's-complement numbers. What's the result, and how does the overflow formula $V = C_8 \oplus C_7$ detect the error?

::: solution
1. **Encode the operands.** −100 = 0x9C and −50 = 0xCE.
2. **Add.**
   $$\text{0x9C} + \text{0xCE} = \text{0x16A}$$
   The low eight bits are 0x6A = 106, with a carry out of the top, $C_8$ = 1.
3. **Find the carry into bit 7.** The low seven bits produce no carry:
   $$\text{0x1C} + \text{0x4E} = \text{0x6A}$$
   so $C_7$ = 0.
4. **Overflow flag.**
   $$\begin{aligned} V &= C_8 \oplus C_7 \\ &= 1 \oplus 0 \\ &= 1 \end{aligned}$$
   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
given: a = 5 | 23 | 60; b = 70 | 100 | 120
answer: a - b + 256
base: 2
width: 8

## Compute {a} − {b} in 8-bit two's complement. Give the result as 8 bits.

::: right
Right. {a} + (inverted {b}) + 1 gives the bit pattern of {a} − {b}.
:::

::: misses
- [b - a] That's the magnitude of the answer, not its two's-complement pattern.
- [any] Not yet. Invert {b}, add 1, add {a}.
:::
:::

::: check
kind: boolean
vars: A, B, S
answer: A & B & ~S | ~A & ~B & S
ops: and, or, not

## A, B and S are the sign bits of two addends and their sum. Write the overflow flag.

::: right
Right. Same signs in, a different sign out.
:::

::: misses
- [A & B | ~A & ~B] Two same-sign numbers can add without overflow; check the sum's sign too.
- [any] Not yet. Overflow needs both inputs alike and the sum different.
:::
:::

::: check
kind: boolean
vars: G1, P1, G0, P0, C0
answer: G1 | P1 & G0 | P1 & P0 & C0
ops: and, or, not

## Write the lookahead carry $C_2$ from $G_1$, $P_1$, $G_0$, $P_0$ and $C_0$.

::: right
Right. $G_1 + P_1G_0 + P_1P_0C_0$.
:::

::: misses
- [G1 | P1 & G0] Include the carry-in, propagated through both bits.
- [any] Not yet. Expand $C_2 = G_1 + P_1C_1$ with $C_1 = G_0 + P_0C_0$.
:::
:::

::: check
given: n = 8 | 16 | 32 | 64; d = 1 | 2
answer: n*d
tolerance: 0.1%

## A ripple-carry adder's carry takes {d} gate delays per bit. What is the worst-case carry delay for {n} bits?

::: right
Right. {answer} gate delays.
:::

::: misses
- [any] Not yet. The carry may cross every bit.
:::
:::

::: check
given: n = 8 | 16 | 32 | 64
answer: log(n)/log(2)
tolerance: 0.1%

## A prefix adder combines generate and propagate in a tree. How many levels does its carry network need for {n} bits?

::: right
Right. $\log_2 n$ = {answer}.
:::

::: misses
- [n] That's a ripple; the tree halves the problem at each level.
- [any] Not yet. Each level doubles the span.
:::
:::

::: check
kind: choice

## An 8-bit adder adds 200 and 100 as unsigned numbers. Which flag shows the result is wrong?

- [x] The carry out
- [ ] The overflow flag
- [ ] The zero flag
- [ ] The negative flag

::: misses
- [2] Overflow is for signed numbers; for unsigned, the carry out means the sum exceeded 255.
- [3] The result, 44, isn't zero.
- [4] Negative is about the signed interpretation.
:::
:::

::: check
kind: parts
given: a = 70 | 100 | 120; b = 60 | 90

## −{a} and −{b} are added as 8-bit two's-complement numbers.

::: part
answer: 256 - a - b
tolerance: 0

### What signed value does the 8-bit result read as?

::: misses
- [-a - b] Below −128 the result wraps around: add 256.
- [any] Keep the low 8 bits.
:::
:::

::: part
answer: 1
tolerance: 0

### What's the overflow flag $C_8 \oplus C_7$?

::: misses
- [0] Two negatives gave a positive: that's overflow.
- [any] The true sum is below −128.
:::
:::
:::

::: check
kind: derivation
vars: n = 4..64, t = 0.000000000001..0.0000000001, k = 2..16

## An n-bit adder is built from gates of delay t.

::: step
answer: 2*n*t

### What's a ripple-carry adder's worst delay, at 2 gate delays per bit?
:::

::: step
answer: 2*k*t + n/k*2*t

### 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
answer: sqrt(n)

### What block size minimizes that delay?
:::

::: right
Right. Blocks of about $\sqrt n$ bits balance the ripple against the chain of selects.
:::
:::

::: explain
## How does carry lookahead avoid waiting for the carry to ripple?

::: model
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

::: check
given: n = 4 | 8 | 16
answer: 2^(n - 1) - 1
tolerance: 0.1%

## What is the largest {n}-bit two's-complement number?

::: misses
- [any] $2^{n-1} - 1$.
:::
:::

::: check
kind: boolean
vars: A, B
answer: A & B
ops: and, or, not

## Write the generate signal for one bit with inputs A and B.

::: misses
- [any] The bit makes a carry on its own.
:::
:::

::: check
given: a = 3 | 9 | 14
answer: 16 - a
base: 2
width: 4

## Write −{a} as a 4-bit two's-complement pattern.

::: misses
- [any] Invert and add 1.
:::
:::
:::
