Make an XOR gate from NAND gates
The XOR gate gives 1 when its inputs differ. It is harder to build than AND or OR, but four NAND gates are enough, and it is a classic exercise in digital electronics courses.
Truth table
| A | B | Y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Try it Click the inputs to change them.
Y0
The idea
The trick is to share one gate. First compute N = A NAND B, which is 0 only when both inputs are 1. Then use N twice: once with A and once with B. Each of those NANDs gives 0 only in the row where its input is 1 and the other is 0. A last NAND joins them and turns those two rows into 1s. Draw the truth table of every wire and you will see it work.
Hints and solution
Try on your own first. Each hint gives away a little more.
- Start with N = A NAND B.
- Then compute A NAND N and B NAND N.
- NAND those two results together. Four gates in total.