Sub topic 15
NAND and NOR Gates and Universality
This chapter assumes you know the OR, AND, and NOT gates and their truth tables, and that you have seen De Morgan's theorems. You should also be comfortable with Boolean expressions such as A + B and A·B. Here you will learn why NAND and NOR are called universal gates and how to build every other gate from either one.
NAND and NOR defined
A NAND gate is an AND gate followed by a NOT gate. Its output is 0 only when both inputs are 1. A NOR gate is an OR gate followed by a NOT gate. Its output is 1 only when both inputs are 0.
| Gate | Boolean expression | Output is 1 when |
|---|---|---|
| NAND | Y = (A·B)' | at least one input is 0 |
| NOR | Y = (A + B)' | both inputs are 0 |
Here the prime (') means NOT. In textbooks you will often see a bar over the expression instead.
Truth tables for both gates:
| A | B | NAND | NOR |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
A quick memory aid: NAND is the opposite of AND, so its output column is 1, 1, 1, 0. NOR is the opposite of OR, so its output column is 1, 0, 0, 0.
The following program prints the truth tables of AND, OR, NAND, and NOR by trying all four input combinations.
from itertools import product
gates = {
"AND": lambda a, b: a & b,
"OR": lambda a, b: a | b,
"NAND": lambda a, b: 1 - (a & b),
"NOR": lambda a, b: 1 - (a | b),
}
for name, gate in gates.items():
print(name)
for a, b in product((0, 1), repeat=2):
print(a, b, "->", gate(a, b))What "universal" means
A gate is universal if you can build every other logic function using only copies of that gate. Since any logic function can be written with AND, OR, and NOT, it is enough to show that a gate can make those three. NAND and NOR both can. This is why digital chips are often built from just one gate type. A single repeated design is cheaper to manufacture than a mix of several.
De Morgan's theorems as the tool
The constructions below rely on De Morgan's two theorems:
- (A·B)' = A' + B'
- (A + B)' = A'·B'
In words, inverting an AND gives an OR of the inverted inputs, and inverting an OR gives an AND of the inverted inputs. You will use them to check each construction.
Building gates from NAND only
NOT from NAND
Tie both inputs of a NAND gate together. If the common input is A, then Y = (A·A)'. Since A·A = A, this becomes Y = A'. So one NAND gate with joined inputs is a NOT gate.
AND from NAND
Pass the output of a NAND gate through a NOT gate. The first gate gives (A·B)'. The second inverts it, so Y = ((A·B)')' = A·B. This takes two NAND gates.
OR from NAND
Invert each input with its own NAND-as-NOT gate. Feed A' and B' into a third NAND gate. The output is (A'·B')'. By De Morgan's second theorem, (A'·B')' = A'' + B'' = A + B. This takes three NAND gates.
The program below builds NOT, AND, and OR from a single nand function and prints the results.
def nand(a, b):
return 1 - (a & b)
def not_(a):
return nand(a, a)
def and_(a, b):
return not_(nand(a, b))
def or_(a, b):
return nand(not_(a), not_(b))
for a in (0, 1):
for b in (0, 1):
print(a, b, "AND:", and_(a, b), "OR:", or_(a, b))The output matches the AND and OR truth tables, which confirms the constructions.
Building gates from NOR only
The NOR constructions mirror the NAND ones, with AND and OR swapped.
NOT from NOR
Join both inputs. Y = (A + A)' = A'.
OR from NOR
Pass the output of a NOR gate through a NOR-as-NOT gate. Y = ((A + B)')' = A + B. This takes two NOR gates.
AND from NOR
Invert each input with its own NOR-as-NOT gate. Feed A' and B' into a third NOR gate. The output is (A' + B')'. By De Morgan's first theorem, (A' + B')' = A''·B'' = A·B. This takes three NOR gates.
Compare the two sets of constructions in one table.
| Target gate | Gates needed using NAND | Gates needed using NOR |
|---|---|---|
| NOT | 1 | 1 |
| AND | 2 | 3 |
| OR | 3 | 2 |
Notice the pattern. NAND makes AND cheaply, and NOR makes OR cheaply. The other one costs three gates in each case.
Alternate gate symbols
By De Morgan's theorems, a NAND gate can be drawn as an OR gate with inverted inputs, because (A·B)' = A' + B'. A NOR gate can be drawn as an AND gate with inverted inputs, because (A + B)' = A'·B'. Circuit diagrams sometimes use these forms, so recognise both. The truth table does not change, only the way you read the symbol.
Worked example: find the output of a combination
Question: two inputs A and B feed a NAND gate. The output of that NAND gate feeds both inputs of a second NAND gate. What single gate does the whole circuit act as?
Step 1: The first gate gives X = (A·B)'.
Step 2: The second gate has joined inputs, so it acts as a NOT gate. Y = X' = ((A·B)')' = A·B.
The circuit is an AND gate. Check with A = 1, B = 1. The first gate gives 0. The second gate inverts it to 1. This matches AND(1, 1) = 1.
Another example: A and B go through NOR gates with joined inputs, giving A' and B'. These feed an AND gate. Y = A'·B'. By De Morgan, A'·B' = (A + B)', so the circuit behaves as a NOR gate.
Common errors
- Mixing up the outputs. A NAND gate is 0 only for input 1, 1. A NOR gate is 1 only for input 0, 0. Students often swap these.
- Forgetting joined inputs make a NOT. A gate with both inputs tied together is a NOT gate, not a two-input gate that you must evaluate separately. For both NAND and NOR, the output is simply the complement of the common input.
- Dropping a bubble. A small circle on a gate symbol means inversion. NAND is AND plus a bubble, and NOR is OR plus a bubble. A missing bubble changes the gate.
- Applying De Morgan incorrectly. When you break the bar, you change the operation as well. (A·B)' becomes A' + B', not A'·B'.
- Counting gates wrongly. Each inverter you build from a NAND or NOR is a full gate. An OR from NAND needs three gates, not one.
Summary of the key facts
- NAND: Y = (A·B)', output 1, 1, 1, 0.
- NOR: Y = (A + B)', output 1, 0, 0, 0.
- Both are universal: each can produce NOT, AND, and OR.
- Joining the inputs of a NAND or NOR gate makes a NOT gate.
- De Morgan's theorems justify every construction.
Exercise
Two inputs A and B each pass through a NOR gate with its inputs joined. The two outputs feed a third NOR gate. Write the Boolean expression for the final output and name the single gate it equals.
Show answer
The first two gates give A' and B'. The third gate gives Y = (A' + B')'. By De Morgan's first theorem, Y = A''·B'' = A·B. The circuit is an AND gate. Check with A = 1, B = 0: the inverted inputs are 0 and 1, and NOR(0, 1) = 0, which equals AND(1, 0) = 0.
Exercise
How many NAND gates are needed to build one OR gate, and what is the output when A = 0 and B = 1?
Show answer
Three NAND gates are needed. Two act as NOT gates to give A' and B', and the third computes (A'·B')'. For A = 0, B = 1, the inverted inputs are 1 and 0. Their NAND is (1·0)' = 1. The output is 1, which equals 0 + 1.
Exercise
The output of a NAND gate with inputs A and B is fed to both inputs of a NOR gate. Find the output Y in terms of A and B, and say which gate this is.
Show answer
The NAND gives X = (A·B)'. A NOR with joined inputs acts as a NOT gate, so Y = X' = A·B. The circuit is an AND gate. For A = 1, B = 1, the NAND gives 0 and the NOT gives 1, which matches AND.