LearnGates

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

Truth table of XOR from NAND
ABY
000
011
101
110

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.

  1. Start with N = A NAND B.
  2. Then compute A NAND N and B NAND N.
  3. NAND those two results together. Four gates in total.