Adders and subtractors
Half and full adders and subtractors, the ripple-carry adder and its delay, carry lookahead, the adder-subtractor with overflow detection, the incrementer and the BCD adder.
Why it exists
Arithmetic in a computer is built from a handful of gates. The adder is the heart of the ALU: subtraction, comparison, address calculation, incrementing the program counter and even multiplication all use it. GATE asks for the equations of the half and full adder, the number of gates, the delay of a ripple-carry adder compared with carry lookahead, how an adder becomes a subtractor and where the overflow bit comes from, and how a BCD adder corrects a binary sum.
Prerequisites
You need the design procedure from combinational design and the meaning of carry and overflow from fixed-point arithmetic.
Precise definitions
Half adder. Adds two bits and . Sum , carry . Cost: 1 XOR and 1 AND. It cannot take a carry from a lower position.
Full adder. Adds three bits , and the carry in :
is the majority of the three inputs. A full adder is two half adders (the first adds and , the second adds the carry in to that sum) and an OR gate for the two carries: 2 XOR + 2 AND + 1 OR = 5 gates.
Generate and propagate. Define (this position generates a carry whatever comes in) and (it propagates an incoming carry). Then and . (Some books use ; the carry is the same.)
Ripple-carry adder. full adders in a row, the carry out of bit feeding the carry in of bit . It is cheap ( full adders, gates) but the carry has to travel through all the stages: the delay grows linearly with . In the model where every gate takes one unit and are ready after one unit, each stage adds two units (AND, then OR) to the carry, so carry is ready after units and the last sum bit about units.
Carry lookahead. Instead of waiting, compute every carry directly from the 's, the 's and . For four bits:
Each is a two-level AND-OR circuit, so in the same model all carries are ready after 3 units ( at 1, AND at 2, OR at 3) and the sums after 4, independent of the number of bits in the group. The price is more gates with larger fan-in. For wide words, lookahead is applied in groups (a second level of lookahead on group and ).
Half subtractor. : difference , borrow out . Full subtractor :
Adder-subtractor. To subtract, use (2's complement). One -bit adder with an XOR gate on each bit of and a control input :
- each goes through ,
- is also the carry into bit 0.
adds () and subtracts (). The carry out is 1 for a subtraction exactly when as unsigned numbers (no borrow). Overflow of the signed operation is (the carries into and out of the sign bit).
Incrementer. A chain of half adders adds 1: , with .
BCD adder. Adds two BCD digits (and a carry in) and gives a BCD digit and a carry. Procedure: add the digits and the carry in with a 4-bit binary adder, giving the 4-bit binary sum and a carry out . If the true sum is more than 9, add 6 (0110) to correct the digit and produce a decimal carry. The correction is needed exactly when
The correction adds 6 because a 4-bit binary digit holds 16 values while a decimal digit uses 10: adding 6 skips the six unused codes and produces the carry into the next digit.
How it works
Step through a 4-bit ripple-carry addition. Each step is one full adder: the three input bits, the sum bit and the new carry. Unknown positions are shown as dots; they are filled in from the right, which is exactly what the circuit does and why the carry limits the speed.
Ripple-carry addition: 1011 + 0110: Set up
Computed exactly, not typedWorking
A = 1011 B = 0110 M = 0 (carry into bit 0)
Value, format and bits (changed bits in amber)
Step 1
Add two 4-bit numbers with four full adders in a row. The carry out of each full adder is the carry into the next one, so the carry has to ripple from bit 0 to bit 3.
The adder becomes a subtractor by feeding the inverted and setting the carry in to 1. Here is : the third row is XOR with .
Adder-subtractor: 0111 - 0101 as 0111 + 1010 + 1: Set up
Computed exactly, not typedWorking
A = 0111 B = 0101 M = 1 (carry into bit 0)
Value, format and bits (changed bits in amber)
Step 1
The control input M = 1 turns the adder into a subtractor: every bit of B is XORed with M, and M is also the carry into bit 0, so the adder computes A + (not B) + 1 = A - B.
The full adder's equations are visible in its truth table: the carry out is the majority of the inputs, and it equals .
Full adder: carry out as majority, and as G + P.Cin
Computed exactly, not typed| A | B | Cin | P=A xor B | G=AB | S | Cout=AB+BCin+ACin | G+P.Cin |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | · | · | · | · | · |
| 0 | 0 | 1 | · | · | · | · | · |
| 0 | 1 | 0 | · | · | · | · | · |
| 0 | 1 | 1 | · | · | · | · | · |
| 1 | 0 | 0 | · | · | · | · | · |
| 1 | 0 | 1 | · | · | · | · | · |
| 1 | 1 | 0 | · | · | · | · | · |
| 1 | 1 | 1 | · | · | · | · | · |
Step 0
Claim: the carry out of a full adder is the majority of its three inputs, and it also equals G + P Cin, where G = AB generates a carry and P = A xor B propagates an incoming one. The sum is P xor Cin.
Worked examples
EX 1The full adder, in a program
EasyThe program models a full adder with and , builds a 4-bit adder-subtractor from it, and checks the ripple adder, the subtractor, the carry-lookahead carries and a BCD adder against ordinary integer arithmetic for every input.
#include <stdio.h>
typedef struct { int sum, cout; } Fa;
static Fa full_adder(int a, int b, int cin) {
int p = a ^ b, g = a & b;
Fa r = { p ^ cin, g | (p & cin) };
return r;
}
/* 4-bit ripple-carry adder-subtractor: mode 0 adds, mode 1 subtracts (b is XORed with mode, mode is the carry in) */
static unsigned add_sub4(unsigned a, unsigned b, int mode, int *cout, int *carry_into_sign) {
unsigned s = 0;
int c = mode;
for (int i = 0; i < 4; i++) {
if (i == 3) *carry_into_sign = c;
Fa r = full_adder((a >> i) & 1, ((b >> i) & 1) ^ mode, c);
s |= (unsigned)r.sum << i;
c = r.cout;
}
*cout = c;
return s;
}
/* 4-bit carry-lookahead carries from p, g and c0 */
static int cla_carry(unsigned a, unsigned b, int c0, int k) {
int c = c0;
for (int i = 0; i < k; i++) {
int p = ((a ^ b) >> i) & 1, g = ((a & b) >> i) & 1;
c = g | (p & c);
}
return c;
}
int main(void) {
int bad_add = 0, bad_sub = 0, bad_cla = 0, bad_bcd = 0;
for (unsigned a = 0; a < 16; a++)
for (unsigned b = 0; b < 16; b++) {
int co, cs;
for (int cin = 0; cin < 2; cin++) {
unsigned t = a + b + (unsigned)cin;
int c = cin;
unsigned s = 0;
for (int i = 0; i < 4; i++) {
Fa r = full_adder((a >> i) & 1, (b >> i) & 1, c);
s |= (unsigned)r.sum << i;
c = r.cout;
}
bad_add += s != (t & 15) || c != (int)(t >> 4);
for (int k = 1; k <= 4; k++) {
unsigned lo = (1u << k) - 1;
bad_cla += cla_carry(a, b, cin, k) != (int)(((a & lo) + (b & lo) + (unsigned)cin) >> k);
}
}
unsigned d = add_sub4(a, b, 1, &co, &cs);
bad_sub += d != ((a - b) & 15) || co != (a >= b);
}
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
for (int cin = 0; cin < 2; cin++) {
int z = x + y + cin;
int carry = z > 15;
int s4 = z & 15;
int fix = carry || s4 > 9;
int res = fix ? s4 + 6 : s4;
int tens = fix ? 1 : 0, ones = res & 15;
bad_bcd += tens * 10 + ones != x + y + cin;
}
printf("ripple add wrong: %d, subtract wrong: %d, lookahead wrong: %d, BCD wrong: %d\n", bad_add, bad_sub, bad_cla, bad_bcd);
int co, cs;
unsigned s = add_sub4(11, 6, 0, &co, &cs);
printf("1011 + 0110 = %u%u%u%u%u (carry out %d)\n", (unsigned)co, (s >> 3) & 1, (s >> 2) & 1, (s >> 1) & 1, s & 1, co);
s = add_sub4(7, 5, 1, &co, &cs);
printf("7 - 5 = %u with carry out %d (no borrow)\n", s, co);
s = add_sub4(5, 7, 1, &co, &cs);
printf("5 - 7 = %u (%d signed) with carry out %d (borrow)\n", s, (int)s - 16, co);
s = add_sub4(7, 1, 0, &co, &cs);
printf("7 + 1 = %u, carry into sign %d, carry out %d, overflow %d\n", s, cs, co, cs ^ co);
return 0;
}ripple add wrong: 0, subtract wrong: 0, lookahead wrong: 0, BCD wrong: 0
1011 + 0110 = 10001 (carry out 1)
7 - 5 = 2 with carry out 1 (no borrow)
5 - 7 = 14 (-2 signed) with carry out 0 (borrow)
7 + 1 = 8, carry into sign 1, carry out 0, overflow 1The first line is a check over all inputs (no mismatches). The following lines are the examples that the animations step through, and the overflow case: does not fit in a signed 4-bit number (maximum 7), and the XOR of the last two carries, , flags it.
EX 2Count the gates of a full adder
EasyFrom the half-adder form: 2 XOR gates ( and then ), 2 AND gates ( and ) and 1 OR gate for the carry: 5 gates. A 4-bit ripple adder with a full adder in every position has gates; with a half adder in position 0 (when there is no carry in) it has gates.
EX 3Subtract with the adder-subtractor
MediumCompute with .
Step 1. , , carry in 1. The animation shows each bit.
Step 2. . The 4-bit result is .
Step 3. The carry out is 1, which for a subtraction means no borrow ().
For the program shows the result unsigned, which is in 2's complement, and a carry out of 0, which means a borrow was needed.
EX 4Overflow detection in the adder-subtractor
Mediumin 4-bit 2's complement: . The carry into the sign bit is 1 and the carry out is 0, so : overflow. The result reads as instead of the true . In contrast has carry in 1 and carry out 1, so .
EX 5Carry-lookahead carries by hand
MediumAdd and with . (bit by bit: , , , ) and (, , , ).
Carries. . . . .
Sums. : , , , . So the sum is with carry out 1, the same as the ripple animation , and every carry was computed without waiting for the previous one.
EX 6Ripple against lookahead delay
GATE levelIn the model "every gate is one unit, and ready after one unit": the 4-bit ripple adder has ready after units (the animation's carry chain, at 3, at 5, at 7, at 9) and the last sum bit after 8; the 4-bit carry-lookahead adder has all carries after 3 units and all sums after 4. For a 32-bit ripple adder the last carry takes units. The lookahead adder avoids this growth but needs gates with up to inputs, which is why wide adders use groups of 4 bits and a second lookahead level.
Always read the question's own delay figures (it may give the delays of AND, OR and XOR separately, or say the carry delay per stage).
EX 7BCD addition with correction
GATE level. Binary: , . has , so the correction condition holds (15 is more than 9). Add 6: , giving the decimal carry 1 and the digit 5: .
. Binary: : and . , so correct: , digit 8, decimal carry 1: .
. . , , so no correction: digit 7, carry 0.
The program checks the whole procedure for all digit pairs and both carry-ins. The largest BCD sum is .
Cheat box
Cheat box
- Half adder: , (2 gates). Full adder: , = majority (5 gates).
- , , , .
- Ripple adder: full adders; delay grows linearly with . Lookahead: all carries from , , in two levels; delay independent of within a group.
- Half subtractor: , . Full subtractor: , .
- Adder-subtractor: XOR each with , carry in . Carry out means no borrow when subtracting.
- Overflow .
- Incrementer: chain of half adders. BCD adder: add, then if sum or carry; condition .
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
Within this subject
The adder-subtractor, with its flags, is the arithmetic core of the ALU. The signed interpretation of the result and the meaning of and come from signed number representation and fixed-point arithmetic. The gates used are those of logic gates (the XOR especially), and the digit code that the BCD adder works on is in codes.
PYQs
7 previous year questions from your book. I re-solved each one myself (5 by running or simulating, 2 by reasoning only); the book's key is shown next to my answer. All PYQs
We consider the addition of two 2's complement numbers bn-1 bn-2 ... b0 and an-1 an-2 ... a0. A binary adder for adding unsigned binary numbers is used to add the two numbers. The sum is denoted by cn-1 cn-2 ... c0 and the carry-out by cout. Which one of the following options correctly identifies the overflow condition?
- (a)cout ( an-1 ⊕bn-1̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅)
- (b)an-1 bn-1cn-1̅̅̅̅̅̅ + an-1̅̅̅̅̅̅ bn-1̅̅̅̅̅̅ cn-1
- (c)cout ⊕ cn-1
- (d)an-1 ⊕ bn-1 ⊕ cn-1
Consider an eight-bit ripple-carry adder for computing the sum of A and B, where A and B are integers represented in 2's complement form. If the decimal value of A is one, the decimal value of B that leads to the longest latency for the sum to stabilize is ______.
Consider a carry look ahead adder for adding two n- bit integers, built using gates of fan-in at most two. The time to perform addition using this adder is:
- (a)Θ(1)
- (b)Θ(log(n))
- (c)Θ(√n)
- (d)Θ(n) DIGITAL LOGIC
A half adder is implemented with XOR and AND gates. A full adder is implemented with two half adders and one OR gate. The propagation delay of an XOR gate is twice that of an AND/OR gate. The propagation delay of an AND/OR gate is 1.2 microseconds. A 4-bit ripple-carry binary adder is implemented by using four full adders. The total propagation time of this 4-bit binary adder in microseconds is ______.
Which one of the following adders reduces carry propagation delay by generating carries in advance?
- (a)Ripple Carry Adder
- (b)Carry Lookahead Adder
- (c)Serial Adder
- (d)Half Adder
In a look-ahead carry generator, the carry generates function Gi and the carry propagate function Pi for inputs Ai and Bi are given by: Pi = Ai ⊕ Bi and Gi = AiBi The expressions for the sum bit Si and the carry bit Ci+1 of the look ahead carry adder are given by: Si = Pi ⊕ Ci and Ci+1 = Gi + PiCi, where C0 is the input carry. Consider a two-level logic implementation of the look-ahead carry generator. Assume that all Pi are available for the carry generator circuit and that the AND and OR gates can have any number of inputs. The number of AND gates and OR gates needed to implement the look-ahead carry generator for a 4-bit adder with S3, S2, S1, S0 and C4 as its outputs are respectively:
- (a)6,3
- (b)10,4
- (c)6,4
- (d)10,5
Given two three-bit numbers a2 a1 a0 and b2 b1 b0 and c the carry in, the function that represents the carry generate function when these two numbers are added is:
- (a)a2b2 + a2a1b1 + a2a1a0b0 + a2a0b1b0 + a1b2b1 + a1a0b2b0 + a0b2b1b0
- (b)a2b2 + a2b1b0 + a2a1b1b0 + a1a0b2b1 + a1a0b2 + a1a0b2b0 + a2a0b1b0
- (c)a2 + b2 + (a2 ⊕ b2) (a1 + b1 + (a1 ⊕ b1)(a0 + b0))
- (d)a2b2 + a̅2a1b1 + a̅2a1a0b0 + a̅2a0b1b0 + a1b2b1 + a̅1a0b2b0 + a0b2b1b0
Self-check
Question 1
How many full adders and how many gates does a 16-bit ripple-carry adder have if every full adder is built from 2 XOR, 2 AND and 1 OR?
Show answerHide
16 full adders and gates.
Question 2
For , , compute , and .
Show answerHide
, . . .
Question 3
In a 4-bit adder-subtractor with , and . What are the result, the carry out and the overflow?
Show answerHide
, carry in 1: with no carry out. The result is in 2's complement (3 - 5), and the carry out 0 means a borrow was needed. The low three bits give , which does not carry into the sign bit, so the carry into the sign bit is 0 and : no overflow.
Question 4
A BCD adder adds the digits 6 and 7 with carry in 0. What are the binary sum, the corrected digit and the decimal carry?
Show answerHide
(13), and , so correct: : digit , decimal carry 1. The result is 13.
Revision card
One-screen revision
- Full adder: , ; 5 gates. Half adder: XOR and AND.
- , ; ripple delay ; lookahead carries in two levels.
- Subtract: XOR with , carry in . Carry out 1 is no borrow. .
- Full subtractor: , .
- BCD adder: binary add, then when sum ().
References
- ·Mano and Ciletti, Digital Design, 5th ed., Chapter 4 Combinational Logic (binary adder-subtractor, carry propagation, decimal adder)
- ·Patterson and Hennessy, Computer Organization and Design, Chapter 3 Arithmetic for Computers (constructing an ALU, carry lookahead)
Verification
The full adder, half subtractor, full subtractor and carry-lookahead equations, the n-bit ripple adder (all operands, n up to 6), the adder-subtractor (result, carry equals no borrow, and overflow for n = 3, 4, 5), the incrementer and the BCD correction condition are checked in scripts/enum-checks/dl_arith.py. The ripple-carry animations are computed by scripts/gen-dl-traces.py and recomputed independently in tests/dl-traces.test.ts. The C program is compiled and run by npm run verify:c. The gate-level delay counts use a stated model (every gate one unit) and may differ from a GATE question's own model. Textbook chapter titles are from memory; the independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (conversions, signed ranges and flags, IEEE 754 fields and custom formats, Boolean identities and counts, K-map groups, adder carries, BCD correction, flip-flop conversions, counter sequences and T equations, state reduction, Quine-McCluskey rounds and covers, floating-point alignment and rounding); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)