Monday, October 30, 2006

XOR Using NAND

Create an XOR gate using only NAND gates.
Solution

By definition:

a XOR b = (a OR b) AND NOT(a AND B)

The key is to apply De Morgan's rule repeatedly:

NOT(x OR y) = (NOT x) AND (NOT y)
NOT(x AND y) = (NOT x) OR (NOT y)

and the fact that:

NOT(x AND y) = x NAND y
NOT x = x NAND x

until XOR can be expressed entirely as NOT and NAND operations:

a XOR b
= (a OR b) AND (a NAND b)
= (NOT(NOT(a OR b))) AND (a NAND b)
= (NOT((NOT a) AND (NOT b))) AND (a NAND b)
= ((NOT a) NAND (NOT b)) AND (a NAND b)
= NOT(((NOT a) NAND (NOT b)) NAND (a NAND b))