Loading lessons...
Adders & Subtractors: Ripple Carry, Adder-Subtractor, Carry Look-Ahead & BCD Adder
Adders & Subtractors: Ripple Carry, Adder-Subtractor, Carry Look-Ahead & BCD Adder
Arithmetic is the most important job of a combinational circuit: the ALU of every CPU is built around an adder. This lesson designs adders and subtractors from the truth table up, shows how one circuit both adds and subtracts, explains why the simple adder is slow and how carry look-ahead fixes it, and builds the BCD adder.
1. Half Adder
A half adder (HA) adds two bits A and B. It produces a sum S and a carry C.
| A | B | C | S |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 0 |
It needs one XOR gate and one AND gate. It is called "half" because it has no carry input, so it can only add the least significant bits.
2. Full Adder
A full adder (FA) adds three bits: A, B and the carry in Cin from the previous position.
| A | B | Cin | Cout | S |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 |
From K-maps: S = ∑ m(1, 2, 4, 7) has no adjacent 1s (a checkerboard), so it does not simplify as SOP but equals the 3-input XOR. Cout = ∑ m(3, 5, 6, 7) simplifies to the majority function.
The second form of Cout shows that a full adder is two half adders and an OR gate: the first HA adds A and B, the second adds Cin to that sum, and the OR combines the two carries.
Half adder, full adder, and a full adder from two half adders
3. Half and Full Subtractor
A half subtractor computes A - B, giving a difference D and a borrow Bo (1 when A < B).
| A | B | Bo | D |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 0 | 0 |
A full subtractor computes A - B - Bin:
| A | B | Bin | Bo | D |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
The difference is the same XOR as the adder's sum; only the borrow differs (it uses A' where the carry uses A). In practice, subtraction is done by an adder with complemented input (Section 5).
4. Ripple-Carry (Parallel) Adder
To add two n-bit numbers, connect n full adders in a chain: the carry out of stage i is the carry in of stage i + 1. This is the ripple-carry adder (or 4-bit parallel binary adder, IC 7483/74283).
4-bit ripple-carry adder
Add A = 1011 and B = 0011 (C0 = 0). Show each stage.
| Stage i | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Ai | 1 | 1 | 0 | 1 |
| Bi | 1 | 1 | 0 | 0 |
| Ci (in) | 0 | 1 | 1 | 0 |
| Si | 0 | 1 | 1 | 1 |
| Ci+1 (out) | 1 | 1 | 0 | 0 |
The weakness: carry propagation delay. S3 is not correct until C3 has arrived, which waits for C2, which waits for C1. In the worst case (for example 0111 + 0001) the carry ripples through all stages. Each full adder's carry takes about 2 gate delays, so an n-bit ripple adder needs about 2n gate delays: 64 gate delays for a 32-bit adder. The adder is the slowest part of the ALU, so this matters.
5. Binary Adder-Subtractor
Subtraction in 2's complement is A - B = A + B' + 1. A single circuit can do both by putting an XOR gate on each B input, controlled by a mode input M:
- M = 0: the XOR passes B unchanged (B oplus 0 = B) and C0 = 0, so the circuit computes A + B.
- M = 1: the XOR inverts B (B oplus 1 = B') and C0 = 1, so the circuit computes A + B' + 1 = A - B.
For signed numbers the overflow output is V = C4 oplus C3 (Lesson 2). For unsigned subtraction, C4 = 1 means A ≥ B (no borrow).
4-bit adder-subtractor with mode input M
The Verilog model below builds the adder-subtractor from four full-adder instances with a generate loop, and also contains a 4-bit carry look-ahead adder (Section 6). The testbench runs four examples and then checks the CLA against a + b for all 256 input pairs.
// Full adder, 4-bit ripple-carry adder-subtractor and 4-bit carry look-ahead adder
module full_adder (
input a, b, cin,
output s, cout
);
assign s = a ^ b ^ cin;
assign cout = (a & b) | (cin & (a ^ b));
endmodule
// M = 0: S = A + B M = 1: S = A - B (A + B' + 1)
module add_sub4 (
input [3:0] a, b,
input m,
output [3:0] s,
output c, v
);
wire [4:0] carry;
assign carry[0] = m;
genvar i;
generate
for (i = 0; i < 4; i = i + 1) begin : stage
full_adder fa (.a(a[i]), .b(b[i] ^ m), .cin(carry[i]), .s(s[i]), .cout(carry[i + 1]));
end
endgenerate
assign c = carry[4];
assign v = carry[4] ^ carry[3]; // signed overflow
endmodule
module cla4 (
input [3:0] a, b,
input c0,
output [3:0] s,
output c4
);
wire [3:0] g = a & b; // generate
wire [3:0] p = a ^ b; // propagate
wire c1 = g[0] | (p[0] & c0);
wire c2 = g[1] | (p[1] & g[0]) | (p[1] & p[0] & c0);
wire c3 = g[2] | (p[2] & g[1]) | (p[2] & p[1] & g[0]) | (p[2] & p[1] & p[0] & c0);
assign c4 = g[3] | (p[3] & g[2]) | (p[3] & p[2] & g[1]) | (p[3] & p[2] & p[1] & g[0])
| (p[3] & p[2] & p[1] & p[0] & c0);
assign s = p ^ {c3, c2, c1, c0};
endmodule
module tb;
reg [3:0] a, b;
reg m;
wire [3:0] s, s2;
wire c, v, c4;
integer i, bad;
add_sub4 u1 (.a(a), .b(b), .m(m), .s(s), .c(c), .v(v));
cla4 u2 (.a(a), .b(b), .c0(1'b0), .s(s2), .c4(c4));
task show;
begin
#1 $display("%b %s %b = %b C=%b V=%b", a, m ? "-" : "+", b, s, c, v);
end
endtask
initial begin
a = 4'b0110; b = 4'b0011; m = 0; show; // 6 + 3
a = 4'b0110; b = 4'b0011; m = 1; show; // 6 - 3
a = 4'b0011; b = 4'b0110; m = 1; show; // 3 - 6
a = 4'b0101; b = 4'b0100; m = 0; show; // 5 + 4
bad = 0;
m = 0;
for (i = 0; i < 256; i = i + 1) begin
{a, b} = i;
#1;
if ({c4, s2} !== a + b) bad = bad + 1;
end
$display("CLA checked on all 256 input pairs, errors = %0d", bad);
end
endmoduleThe simulation output:
0110 + 0011 = 1001 C=0 V=1
0110 - 0011 = 0011 C=1 V=0
0011 - 0110 = 1101 C=0 V=0
0101 + 0100 = 1001 C=0 V=1
CLA checked on all 256 input pairs, errors = 0Read it carefully. 6 + 3 = 9 is correct as an unsigned result (1001), but as a 4-bit signed result it overflows (range −8 to +7), so V = 1. 6 - 3 = 3 produces C = 1 (no borrow). 3 - 6 = 1101 = -3 in 2's complement, with C = 0 (a borrow occurred).
6. Carry Look-Ahead Adder (CLA)
The CLA computes every carry directly from the inputs in two gate levels instead of waiting for it to ripple. Define for each bit position:
- Gi = 1: position i creates a carry whatever comes in.
- Pi = 1: position i passes an incoming carry on.
So Ci+1 = Gi + Pi Ci and Si = Pi oplus Ci. Substituting repeatedly removes the chain:
Each carry is a sum of products, so it takes two gate levels after G and P are formed. The total delay of a 4-bit CLA is about 4 gate delays (1 for G/P, 2 for the carries, 1 for the sum XOR), independent of which carry it is.
Carry look-ahead generator and adder
| Feature | Ripple-carry adder | Carry look-ahead adder |
|---|---|---|
| Carry computed | stage by stage | all at once from G and P |
| Delay | grows linearly, about 2n gate delays | about 4 gate delays for 4 bits |
| Hardware | n full adders, simple | extra look-ahead logic; gates need many inputs |
| Use | small or slow adders | fast ALUs (in groups of 4, cascaded) |
For A = 1011, B = 0110, C0 = 0 find Gi, Pi, all carries and the sum using the CLA equations.
- G = A · B = 0010 (only bit 1: 1 · 1). P = A oplus B = 1101.
- C1 = G0 + P0 C0 = 0 + 1 · 0 = 0.
- C2 = G1 + P1 G0 + P1 P0 C0 = 1.
- C3 = G2 + P2 G1 + ldots = 0 + 1 · 1 = 1.
- C4 = G3 + P3 G2 + P3 P2 G1 + ldots = 0 + 0 + 1 · 1 · 1 = 1.
- Si = Pi oplus Ci: S0 = 1 oplus 0 = 1, S1 = 0 oplus 0 = 0, S2 = 1 oplus 1 = 0, S3 = 1 oplus 1 = 0.
For wider adders the look-ahead logic gets too big (gates with many inputs), so 4-bit CLA blocks are combined with a second level of look-ahead using group generate G^* = G3 + P3 G2 + P3 P2 G1 + P3 P2 P1 G0 and group propagate P^* = P3 P2 P1 P0 (IC 74182).
7. BCD Adder
A BCD adder adds two BCD digits (0 to 9) plus a carry, giving a BCD digit and a decimal carry. The binary sum ranges from 0 to 9 + 9 + 1 = 19.
- Add the two digits with a 4-bit binary adder: carry K and sum Z8 Z4 Z2 Z1.
- If the binary sum is more than 9, add 6 (0110) to correct it and produce a decimal carry.
- The sum exceeds 9 when K = 1 (sums 16 to 19) or when Z8 Z4 Z2 Z1 is 1010 to 1111:
- A second 4-bit adder adds 0 C C 0 to Z. Its carry out is ignored (C is the decimal carry).
BCD adder with correction logic
// One-digit BCD adder: binary add, then add 6 when the sum exceeds 9
module bcd_adder (
input [3:0] a, b,
input cin,
output [3:0] s,
output cout
);
wire [4:0] z = a + b + cin; // first 4-bit adder (K = z[4])
assign cout = z[4] | (z[3] & z[2]) | (z[3] & z[1]); // C = K + Z8 Z4 + Z8 Z2
assign s = z[3:0] + {1'b0, cout, cout, 1'b0}; // second adder adds 0110 or 0000
endmodule
module tb;
reg [3:0] a, b;
reg cin;
wire [3:0] s;
wire cout;
bcd_adder dut (.a(a), .b(b), .cin(cin), .s(s), .cout(cout));
task show;
begin
#1 $display("%0d + %0d + %0d -> carry %b, digit %b (%0d%0d)", a, b, cin, cout, s, cout, s);
end
endtask
initial begin
a = 4; b = 5; cin = 0; show;
a = 4; b = 6; cin = 0; show;
a = 8; b = 7; cin = 1; show;
a = 9; b = 9; cin = 1; show;
end
endmoduleThe decimal result appears as the carry followed by the digit:
4 + 5 + 0 -> carry 0, digit 1001 (09)
4 + 6 + 0 -> carry 1, digit 0000 (10)
8 + 7 + 1 -> carry 1, digit 0110 (16)
9 + 9 + 1 -> carry 1, digit 1001 (19)Key points to remember
- HA: S = A oplus B, C = AB. FA: S = A oplus B oplus Cin, Cout = AB + Cin(A oplus B); FA = 2 HA + OR.
- Subtractor borrow uses A': Bo = A'B (half), A'B + Bin(A oplus B)' (full).
- Adder-subtractor: XOR each Bi with M and set C0 = M. Overflow V = Cn oplus Cn-1.
- CLA: Gi = Ai Bi, Pi = Ai oplus Bi, Ci+1 = Gi + Pi Ci expanded to two levels.
- BCD adder: correction (add 6) when C = K + Z8 Z4 + Z8 Z2 = 1.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Design a full adder using two half adders and an OR gate. Derive the sum and carry expressions with K-maps. (10 Marks)
- Q2. Design a full subtractor. Give its truth table, K-maps and logic diagram. (5 Marks)
- Q3. Draw a 4-bit binary adder-subtractor and explain its working for M = 0 and M = 1. How is overflow detected? (10 Marks)
- Q4. What is a carry look-ahead adder? Derive the carry expressions C1 to C4 and compare it with a ripple-carry adder. (10 Marks)
- Write: the G and P definitions, all four carry equations, the delay comparison table.
- Q5. Design a BCD adder and explain the correction logic. (10 Marks)