Sub topic 15
NAND and NOR Gates and Universality
This chapter assumes you know the truth tables of the OR, AND, and NOT gates and can read a Boolean expression such as Y = A·B. If those are shaky, review the chapter on basic gates first.
NAND and NOR gates
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.
| A | B | NAND (Y = (A·B)') | NOR (Y = (A+B)') |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
Here a prime (') means NOT, so (A·B)' is the complement of A·B. In NEET papers you will also see a bar drawn over the expression.
The following program prints the truth table of each two-input gate by evaluating it on 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 gate from copies of it alone. NAND and NOR are both universal. This matters in practice because a chip can then be made from a single gate type, which keeps manufacturing simple.
Building gates from NAND only
Three constructions cover everything.
- NOT: join both inputs of a NAND gate. Then Y = (A·A)' = A'.
- AND: put a NOT after a NAND. Then Y = ((A·B)')' = A·B.
- OR: invert each input with a NAND-made NOT, then feed both into a NAND. Then Y = (A'·B')' = A + B.
The last step uses De Morgan's theorem: (A'·B')' = A + B.
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 on all four rows.
Building gates from NOR only
The NOR gate works the same way, with the roles of AND and OR swapped.
- NOT: join the inputs. Y = (A+A)' = A'.
- OR: put a NOT after a NOR. Y = ((A+B)')' = A + B.
- AND: invert each input, then feed both into a NOR. Y = (A'+B')' = A·B.
Common errors
- Reading NAND as "NOT and AND" and inverting the inputs. The inversion applies to the output.
- Forgetting that joined inputs make NAND and NOR behave as NOT.
- Mixing up which gate gives 1 only for all-zero inputs. That is NOR. NAND gives 0 only for all-one inputs.
Exercise
Two inputs A and B go into a NOR gate. The output goes to both inputs of a second NOR gate. Write the final output in terms of A and B and name the equivalent gate.
Show answer
The first gate gives (A+B)'. The second gate has joined inputs, so it acts as NOT and gives ((A+B)')' = A + B. The combination is an OR gate.
Exercise
A student has only NAND gates. How many are needed to build one OR gate, and what is the output when A = 1 and B = 0?
Show answer
Three NAND gates are needed: two joined-input NANDs to invert A and B, and one NAND to combine them. With A = 1 and B = 0, the inverted inputs are 0 and 1, and NAND(0, 1) = 1. So the output is 1.