CE2704 · Digital Logic Design
Theme 4 · Combinational building blocks

The adder/subtractor

One control bit turns your adder into a subtractor too — no second circuit, just two's complement and a row of XOR gates.

Built from first principles.

Before you start

What you need first

  • Two's complement — negate by "invert all bits, then add 1".
  • The ripple-carry adder — the n-bit adder we'll reuse.
  • XOR gate — used here as a controllable inverter.

What you'll be able to do

  • Subtract using an adder: A − B = A + B′ + 1.
  • Use XOR as a controlled inverter driven by a mode bit M.
  • Read the one-circuit adder/subtractor and trace add vs subtract.

Subtract by adding the complement

From two's complement, negating B means invert every bit and add 1. So subtraction is just an addition in disguise:

$$ A - B = A + (\text{NOT } B) + 1 = A + B' + 1 $$

We already have an n-bit adder. To subtract, we need two changes: feed it B′ (every bit of B inverted) instead of B, and add the extra +1 by setting the adder's carry-in to 1. Both can be switched by a single mode bit M.

XOR: an inverter you can switch on

The key trick is one property of XOR:

$$ B \oplus 0 = B \qquad\qquad B \oplus 1 = B' $$

So Bi ⊕ M passes B straight through when M = 0, and inverts it when M = 1. Put one XOR on each B bit, tie the other input of every XOR to M, and also feed M into the carry-in. That one bit now chooses the operation:

n-bit ADDER B A B⊕M Cin S Cout M ×n
One XOR per B bit (×n), all controlled by M; M also sets the carry-in. M = 0 adds, M = 1 subtracts.

What M does

MB into adderCarry-inAdder computesOperation
0B ⊕ 0 = B0A + B + 0add
1B ⊕ 1 = B′1A + B′ + 1subtract (A − B)
Same gates, same adder — the mode bit M flips it between + and − for free. This is exactly the add/subtract path inside an ALU.
📐 Worked example

Compute 6 − 3 in 4-bit two's complement (M = 1)

Take A = 0110 (6) and B = 0011 (3). With M = 1 the circuit inverts B and sets carry-in to 1:

$$ B' = \text{NOT } 0011 = 1100,\qquad C_{in} = 1 $$

Now the adder computes A + B′ + 1:

$$ 0110 + 1100 + 1 = 1\,0011_2 $$

That's 5 bits. In 4-bit two's complement we drop the carry-out (the leading 1):

$$ \text{result} = 0011_2 = 3 $$
6 − 3 = 3. ✓ Subtraction done with nothing but an adder, some XORs, and one control bit.

✏️ Try it yourself

(a) Rewrite A − B as an addition. (b) What is B ⊕ 1? (c) What two things does setting M = 1 do? (d) Compute 5 − 5 in 4 bits by the complement method — give the 4-bit result.

(a) A − B = A + B′ + 1 (add the two's complement of B). (b) B′ — XOR with 1 inverts the bit. (c) inverts every B bit (via the XORs) and sets the carry-in to 1. (d) 0101 + 1010 + 1 = 1 0000 → drop carry → 0000 (= 0).

Recap — the whole topic on one screen

IdeaWhat you own now
Subtract = addA − B = A + B′ + 1
Controlled inverterB ⊕ M: pass when M=0, invert when M=1
The circuitn XORs on B + an n-bit adder, with M tied to carry-in
Mode bitM = 0 adds, M = 1 subtracts

Next topic

The magnitude comparator

Addition and subtraction are done. Next: a block that doesn't compute a value but a verdict — is A greater than, equal to, or less than B?

→ The magnitude comparator