Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 31 of 9361. Foundations of Digital Logic and Computer ArchitectureFree lesson

Performing Binary Subtraction with Two's Complement

Learners will modify adder hardware to perform subtraction by implementing two's complement arithmetic using bitwise inversion and carry-in manipulation.

In this lesson, you will learn how to transform your existing adder hardware into a subtractor by leveraging the mathematical elegance of two’s complement arithmetic.

The Logic of Subtraction

In digital hardware, building a separate circuit for subtraction is inefficient. Instead, we use the property that subtracting a number B from A is identical to adding the negative of B to A (A - B = A + (-B)).

To find the negative of a binary number (its two's complement), we follow two simple steps:

  1. Invert the bits: Flip every 0 to a 1 and every 1 to a 0 (the bitwise NOT).
  2. Add one: Add a 1 to the least significant bit.

Modifying the Adder

Since you already know how to build a Ripple Carry Adder, we can perform this conversion at the hardware level using a single control signal, let’s call it SUB.

We pass each bit of the subtrahend B through an XOR gate before it enters the adder. If SUB is 0, the XOR output is just B (adding normally). If SUB is 1, the XOR effectively inverts the bits of B.

To complete the "add one" part of the two's complement, we connect the SUB signal to the initial Carry-In (CinC_{in}) of the adder. When SUB is active, the adder treats the inverted bits as negative and automatically adds that extra 1 via the carry input.

Hardware Illustration

Imagine a single bit slice of this circuit:

// XOR gate handles bit inversion
// 'SUB' acts as the control signal
InputB_Inverted = B XOR SUB;

// Full Adder uses the modified bit
{CarryOut, Sum} = FullAdder(A, InputB_Inverted, SUB);

By toggling the SUB wire, your arithmetic circuit instantly switches from addition to subtraction. This efficiency is why modern ALUs (Arithmetic Logic Units) don't carry separate hardware for subtraction; they simply wrap the adder in this clever logic, saving transistors and board space.

Key Takeaway: By using XOR gates to invert bits and feeding the operation signal into the carry-in, you can convert any adder into a subtractor using two's complement arithmetic.