XOR gates either pass the number through (when control = 0) or invert all bits (when control = 1)
This is the FIRST STEP of 2's complement: invert the bits
The first adder adds the two prepared numbers
The carry input is set to 1 when either SA or SB is 1
This is the SECOND STEP of 2's complement: add 1
This works perfectly for scenarios 1, 2, and 3!
When BOTH SA and SB are 1, we need TWO 2's complements
Stage 2 only added +1, so we're SHORT by 1
The second adder adds that missing +1 only when (SA AND SB) = 1
For other scenarios, it just passes the result through (adds +0)
| SA | SB | Operation | Stage 1 Output | Stage 2 Output | Stage 3 Adds | Final Result |
|---|---|---|---|---|---|---|
| 0 | 0 | X + Y | A, B | A + B + 0 | +0 | X + Y ✓ |
| 0 | 1 | X - Y | A, NOT B | A + (NOT B) + 1 | +0 | X - Y ✓ |
| 1 | 0 | -X + Y | NOT A, B | (NOT A) + B + 1 | +0 | -X + Y ✓ |
| 1 | 1 | -X - Y | NOT A, NOT B | (NOT A) + (NOT B) + 1 | +1 | -X - Y ✓ |
A multiplexer (MUX) is like a switch that selects between two inputs. You haven't learned about it yet, so we use what you already know: adders!
By feeding 0000 to one input of the second adder, we effectively get:
This is just adding 0 or 1 based on the carry in, which is exactly what we need.
Same result, simpler components!