Sub topic 18
Building Any Gate from NAND or NOR
This chapter assumes you know the truth tables of the OR, AND, NOT, NAND and NOR gates from the previous chapter. It also assumes you can read Boolean expressions, where A + B means OR, A·B means AND, and a bar over a term (written A' here) means NOT.
Why one gate is enough
A gate is called universal if every other gate can be built from copies of it alone. NAND and NOR are both universal. This matters in two ways:
- In hardware, a chip made of one repeated gate type is simpler to design and manufacture.
- In JEE Main, a standard question gives a circuit of NAND or NOR gates, often with inputs joined together, and asks which single gate it is equivalent to.
Recall the two truth tables:
| A | B | NAND | NOR |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
NAND gives 0 only when both inputs are 1. NOR gives 1 only when both inputs are 0.
The two building tools
Every construction below uses one of two tricks.
- Tie the inputs together. If both inputs of a NAND are connected to A, the output is NAND(A, A) = (A·A)' = A'. The gate becomes a NOT gate. The same holds for NOR, since (A + A)' = A'.
- Invert an output. Passing any output through a NOT gate turns NAND into AND, and NOR into OR. The reverse also works.
The OR and AND constructions rest on De Morgan's laws:
- (A·B)' = A' + B'
- (A + B)' = A'·B'
Building every gate from NAND
NOT, AND and OR
- NOT: tie both inputs together. Y = NAND(A, A) = A'.
- AND: a NAND followed by a NOT. Y = ((A·B)')' = A·B. This uses 2 gates.
- OR: invert each input first, then apply NAND. Y = NAND(A', B') = (A'·B')' = A + B by De Morgan's law. This uses 3 gates.
- NOR: build OR, then invert it. This uses 4 gates.
Check by truth table
For the OR construction, follow each row:
| A | B | A' | B' | NAND(A', B') | A OR B |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | 1 | 1 |
The last two columns match, so the circuit is an OR gate.
Running the construction in code
The function below models NAND on the integers 0 and 1. Every other gate is defined only by calling nand.
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))
print("A B | AND OR")
for a in (0, 1):
for b in (0, 1):
print(a, b, "|", and_(a, b), " ", or_(a, b))The printed rows are the AND and OR truth tables. For example, the row 0 1 | 0 1 shows AND = 0 and OR = 1.
Building every gate from NOR
The NOR constructions mirror the NAND ones, with AND and OR swapped.
- NOT: tie the inputs together. Y = NOR(A, A) = A'.
- OR: a NOR followed by a NOT. Y = ((A + B)')' = A + B. This uses 2 gates.
- AND: invert each input first, then apply NOR. Y = NOR(A', B') = (A' + B')' = A·B. This uses 3 gates.
- NAND: build AND, then invert it. This uses 4 gates.
| Target gate | Using NAND | Using NOR |
|---|---|---|
| NOT | 1 | 1 |
| AND | 2 | 3 |
| OR | 3 | 2 |
| NOR | 4 | 1 (itself) |
| NAND | 1 (itself) | 4 |
A harder case: XOR from NAND
XOR gives 1 when the inputs differ. Its expression is A·B' + A'·B. A well-known circuit builds it from four NAND gates:
- N1 = NAND(A, B)
- N2 = NAND(A, N1)
- N3 = NAND(B, N1)
- Y = NAND(N2, N3)
The code below builds XOR this way. It then builds the NOR versions of NOT, OR and AND, and checks every result against Python's own operators.
def nand(a, b):
return 1 - (a & b)
def nor(a, b):
return 1 - (a | b)
def xor_from_nand(a, b):
n1 = nand(a, b)
n2 = nand(a, n1)
n3 = nand(b, n1)
return nand(n2, n3)
def not_nor(a):
return nor(a, a)
def or_nor(a, b):
return not_nor(nor(a, b))
def and_nor(a, b):
return nor(not_nor(a), not_nor(b))
for a in (0, 1):
for b in (0, 1):
assert xor_from_nand(a, b) == (a ^ b)
assert or_nor(a, b) == (a | b)
assert and_nor(a, b) == (a & b)
assert not_nor(a) == 1 - a
print("All NAND and NOR constructions match.")If a construction were wrong, an assert would fail and show a traceback. When all of them hold, the script prints the final line.
Reading a circuit in an exam
Work from the inputs toward the output and write the value after every gate. Use this routine on any given circuit.
- Check whether any gate has its inputs tied. If so, replace it with a NOT.
- Write the Boolean expression at each gate output.
- Simplify with De Morgan's laws.
- Compare the final expression with the known gates, or test it with all four input rows.
Worked example. Gate 1 is a NAND with inputs A and B. Its output feeds both inputs of gate 2, also a NAND.
- Gate 1 output: (A·B)'
- Gate 2 output: NAND of the same signal twice, which is the NOT of (A·B)', so the result is A·B.
The circuit is an AND gate.
Why this is practical
Real chips are made from transistors, and a NAND or NOR gate can be formed from just a few of them. Because any logic function reduces to one of these gates, a manufacturer can lay out one gate pattern millions of times. The transistor action from earlier chapters, where a small base current switches a larger collector current, is what makes each gate switch between the two logic levels.
Exercise
What is the minimum number of 2-input NOR gates needed to build a 2-input AND gate, and what does each gate do?
Show answer
Three. Two NOR gates have their inputs tied, one for A and one for B, so they give A' and B'. The third NOR takes A' and B' as inputs and gives (A' + B')' = A·B.
Exercise
A NAND gate has inputs A and B. Its output goes to both inputs of a second NAND gate. The second gate's output is Y. Find Y for A = 1, B = 0, and name the equivalent gate.
Show answer
The first gate gives NAND(1, 0) = 1. The second gate has both inputs equal to 1, so Y = NAND(1, 1) = 0. Checking all rows shows Y = A·B, so the circuit is an AND gate, and A = 1, B = 0 gives AND = 0, which matches.
Exercise
Show using De Morgan's law that NAND(A', B') equals A + B.
Show answer
NAND(A', B') = (A'·B')'. De Morgan's law gives (A'·B')' = (A')' + (B')' = A + B.