Sub topic 6
Half adders, full adders and subtractors
This chapter assumes you know binary addition, 2's complement representation, and the XOR, AND and OR gates. It also assumes you can write a Boolean expression from a truth table. Everything here is combinational, so the outputs depend only on the current inputs.
Why arithmetic circuits matter
Adders are the first standard block built from the gates and Boolean algebra covered earlier. A subtractor is an adder with a small change. GATE questions in this area usually ask for one of four things: a Boolean expression for an output, a gate count, a way to build one block from another, or the overflow condition.
Half adder
A half adder adds two 1-bit numbers, A and B. It produces a sum bit S and a carry bit C. It is "half" because it has no input for a carry from a lower bit.
| A | B | S | C |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
From the table:
- S = A ⊕ B
- C = A · B
The sum is 1 when exactly one input is 1. The carry is 1 only when both are 1. The circuit needs one XOR gate and one AND gate.
Full adder
A full adder adds three 1-bit values: A, B and a carry-in Cin. The result is a sum bit S and a carry-out Cout. Chaining full adders gives a multi-bit adder, because each stage can accept the carry from the stage below.
| A | B | Cin | S | Cout |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
S is 1 when an odd number of inputs are 1, so S is the three-input XOR. Cout is 1 when at least two inputs are 1, so Cout is the majority function.
- S = A ⊕ B ⊕ Cin
- Cout = AB + ACin + BCin
- Equivalent form: Cout = AB + Cin(A ⊕ B)
The second form for Cout is the one used in hardware. It shows that a full adder is two half adders and an OR gate.
Check this against the expression: the first half adder gives A ⊕ B and AB. The second takes A ⊕ B and Cin, giving S = A ⊕ B ⊕ Cin and the carry Cin(A ⊕ B). The OR gate combines the two carries into Cout = AB + Cin(A ⊕ B).
Gate counts
| Block | Gates (basic) | NAND-only |
|---|---|---|
| Half adder | 1 XOR, 1 AND | 5 NAND |
| Full adder | 2 XOR, 2 AND, 1 OR | 9 NAND |
The NAND-only half adder has four NAND gates for the XOR, plus one more NAND to invert (AB)', which the XOR already computes internally. The 9-NAND full adder is two of these half adders with the final OR absorbed. Universal-gate counts like these are a common GATE question.
Verilog
The full adder from the topic plan, written with continuous assignments:
module full_adder(input a, input b, input cin, output sum, output cout);
assign sum = a ^ b ^ cin;
assign cout = (a & b) | (cin & (a ^ b));
endmoduleThe same circuit built structurally from two half adders:
module half_adder(input a, input b, output s, output c);
assign s = a ^ b;
assign c = a & b;
endmodule
module full_adder_ha(input a, input b, input cin, output sum, output cout);
wire s1, c1, c2;
half_adder h1(.a(a), .b(b), .s(s1), .c(c1));
half_adder h2(.a(s1), .b(cin), .s(sum), .c(c2));
assign cout = c1 | c2;
endmoduleSubtractors
A half subtractor computes A − B for two bits. It produces a difference D and a borrow Bout, which is 1 when A < B and a borrow from the next higher bit is needed.
| A | B | D | Bout |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
- D = A ⊕ B
- Bout = A' · B
The half subtractor differs from the half adder in one place: the borrow uses A complemented. That costs one extra NOT gate.
A full subtractor computes A − B − Bin, where Bin is the borrow from the lower bit.
| A | B | Bin | D | Bout |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
- D = A ⊕ B ⊕ Bin
- Bout = A'B + A'Bin + B·Bin
- Equivalent form: Bout = A'B + (A ⊕ B)' · Bin
Subtraction with an adder
Real hardware rarely uses separate subtractors. In 2's complement, A − B = A + (~B) + 1. An adder can therefore subtract if the B input is inverted and the carry-in is set to 1.
One control signal M selects the operation. Each bit of B passes through an XOR with M, and M also feeds the first carry-in.
- M = 0: B ⊕ 0 = B and Cin = 0, so the circuit computes A + B.
- M = 1: B ⊕ 1 = B' and Cin = 1, so the circuit computes A + B' + 1 = A − B.
A 4-bit adder/subtractor with carry-out and overflow flags:
module addsub4(input [3:0] a, input [3:0] b, input m,
output [3:0] y, output cout, output ovf);
wire [3:0] bx = b ^ {4{m}};
wire [4:0] sum = a + bx + m;
wire c3 = a[3] ^ bx[3] ^ sum[3]; // carry into the MSB
assign y = sum[3:0];
assign cout = sum[4];
assign ovf = c3 ^ cout;
endmoduleCarry-out and borrow
When subtracting with an adder, the carry-out is the inverse of the borrow. For unsigned numbers, Cout = 1 means A ≥ B, so no borrow occurred. Cout = 0 means A < B and the result wrapped around. Mixing up this polarity is a frequent source of wrong answers.
Overflow in signed arithmetic
Overflow means the true result does not fit in n bits of 2's complement. It can only happen when both operands have the same sign (for addition) and the result has the opposite sign. Two equivalent tests:
- Overflow = carry into the MSB XOR carry out of the MSB.
- Overflow = the sign bits of the operands match and the sign bit of the result differs.
Unsigned overflow is different. It is simply Cout = 1 on addition, and it has nothing to do with the sign test above.
Delay in a ripple chain
Chaining n full adders gives a ripple-carry adder. The carry must travel through every stage, so the worst-case delay grows linearly with n. Inside one stage, the carry path from Cin to Cout passes through an AND and an OR, which is two gate delays. A 4-bit ripple adder therefore has a carry path of roughly 8 gate delays. The next chapter on carry-lookahead addition removes this linear growth.
Common errors
- Writing Cout as A ⊕ B ⊕ Cin. That is S, not Cout.
- Using B' instead of A' in the borrow expression A'B.
- Reading the carry-out of a subtraction as a borrow, when it is the inverse of one.
- Applying the carry-in/carry-out overflow test to unsigned numbers.
- Forgetting the +1 when subtracting with the invert-and-add method.
Exercise
Using the 4-bit adder/subtractor with M = 1, compute 0110 − 0101. Give the 4-bit result and the carry-out, and say whether a borrow occurred.
Show answer
B' = 1010 and Cin = 1. The sum is 0110 + 1010 + 1 = 10001. The 4-bit result is 0001 and Cout = 1. Since Cout = 1, no borrow occurred, which is correct because 6 ≥ 5.
Exercise
A 4-bit 2's complement adder computes 0111 + 0011. Find the result and decide whether overflow occurred, using carries into and out of the MSB.
Show answer
0111 + 0011 = 1010. The carry into bit 3 is 1 because 1 + 1 + 0 at bit 2 produces a carry (bit 2 has 1 + 0 + carry-in 1). The carry out of bit 3 is 0 because 0 + 0 + 1 = 1 with no carry. The two carries differ, so overflow occurred: 7 + 3 = 10 does not fit in the range −8 to 7.
Exercise
A full subtractor has A = 0, B = 1, Bin = 1. Give D and Bout, and show the answer is consistent with a full adder.
Show answer
D = 0 ⊕ 1 ⊕ 1 = 0. Bout = A'B + A'Bin + B·Bin = 1 + 1 + 1 = 1. Numerically 0 − 1 − 1 = −2, which is D = 0 with a borrow of 1 (borrow worth 2). With a full adder on inputs A' = 1, B = 1, Bin = 1, the sum is 1 ⊕ 1 ⊕ 1 = 1 and the carry is 1. The carry matches Bout. The difference bit equals the sum of the original A, B, Bin (0 ⊕ 1 ⊕ 1 = 0).