Sub topic 7
Universal gates: NAND-only and NOR-only designs
This chapter assumes you know the truth tables of the basic gates, De Morgan's laws, and sum-of-products (SOP) and product-of-sums (POS) forms from the earlier chapters. The goal is to build any Boolean function from one gate type and to count the gates that takes.
What "universal" means
A set of operations is functionally complete if every Boolean function can be written using only those operations. {AND, OR, NOT} is complete because every function has a sum-of-minterms form. A gate is universal if it forms a complete set by itself. NAND and NOR are the two universal gates among the standard two-input gates.
Completeness follows from a short argument. If you can build NOT, AND and OR from a gate, you can build everything. The sections below do this for NAND and NOR.
Some sets are not complete. {AND, OR} cannot produce NOT, because any circuit built from them outputs 1 when all inputs are 1. XOR alone is not complete either, because it cannot produce the constant 1 from the inputs.
Building the basic gates from NAND
The NAND gate computes (AB)'. Two identities are enough to build the rest:
- Tying both inputs together gives NOT: NAND(A, A) = (AA)' = A'.
- Inverting a NAND output gives AND: AND(A, B) = NAND(NAND(A, B), NAND(A, B)).
For OR, apply De Morgan: A + B = (A'B')' = NAND(A', B').
| Function | NAND-only form | 2-input NAND gates |
|---|---|---|
| NOT A | NAND(A, A) | 1 |
| A AND B | NOT(NAND(A, B)) | 2 |
| A OR B | NAND(A', B') | 3 |
| A NOR B | NOT(OR) | 4 |
| A XOR B | NAND(NAND(A, t), NAND(B, t)), where t = NAND(A, B) | 4 |
| A XNOR B | NOT(XOR) | 5 |
The XOR construction shares the gate t between two branches. This sharing is why XOR costs 4 gates and not more.
Building the basic gates from NOR
NOR is the dual of NAND, so every construction swaps AND with OR. NOR computes (A + B)'.
- NOT A = NOR(A, A).
- A + B = NOT(NOR(A, B)), which costs 2 gates.
- AB = (A' + B')' = NOR(A', B'), which costs 3 gates.
| Function | NOR-only form | 2-input NOR gates |
|---|---|---|
| NOT A | NOR(A, A) | 1 |
| A OR B | NOT(NOR(A, B)) | 2 |
| A AND B | NOR(A', B') | 3 |
| A NAND B | NOT(AND) | 4 |
| A XNOR B | NOR(NOR(A, t), NOR(B, t)), where t = NOR(A, B) | 4 |
| A XOR B | NOT(XNOR) | 5 |
Note that the 4-gate XOR belongs to NAND and the 4-gate XNOR belongs to NOR.
Checking the constructions in code
The script below builds the gates from a single nand function and compares each one against Python's own operators on every input row. This is the truth-table equivalence check from an earlier chapter.
from itertools import product
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))
def xor(a, b):
t = nand(a, b)
return nand(nand(a, t), nand(b, t))
rows = list(product([0, 1], repeat=2))
print(all(not_(a) == 1 - a for a, _ in rows))
print(all(and_(a, b) == (a & b) for a, b in rows))
print(all(or_(a, b) == (a | b) for a, b in rows))
print(all(xor(a, b) == (a ^ b) for a, b in rows))All four lines print True.
Two-level designs: SOP to NAND-NAND
Converting an SOP expression gate by gate would waste inverters. A shortcut exists because of De Morgan's law. Take F = AB + CD:
- Apply a double complement: F = ((AB + CD)')'.
- Apply De Morgan inside: F = ((AB)' · (CD)')'.
The outer operation is a NAND of two terms, and each inner term is a NAND of the variables in one product. So an AND-OR circuit becomes a NAND-NAND circuit with the same wiring.
The rule is: one first-level NAND per product term, plus one output NAND. For k product terms with more than one literal, the cost is k + 1 gates, assuming gates of any fan-in and all inputs available in true form.
Single-literal terms
A product term with one literal needs no gate of its own. Take F = AB + C'. The term C' would pass through a one-input NAND, which is an inverter, giving (C')' = C. So feed C directly into the output NAND:
F = NAND(NAND(A, B), C)
Check: ((AB)' · C)' = AB + C'. This circuit uses 2 gates. Whenever a single-literal term is complemented, it enters the output gate in true form. Whenever it is uncomplemented, it needs an inverter first.
Complemented inputs
If a product term contains a complemented variable, such as A'B, you need A' as an input. Unless the problem says complemented inputs are available, add one inverter per variable that appears complemented. For F = A'B + AC, the count is:
- 1 inverter for A'
- 2 first-level NAND gates
- 1 output NAND gate
That makes 4 gates.
Two-level designs: POS to NOR-NOR
The dual argument handles POS. For F = (A + B)(C + D):
F = ((A + B)(C + D))'' = ((A + B)' + (C + D)')'
This is NOR(NOR(A, B), NOR(C, D)), a 3-gate circuit. One first-level NOR gate is used per sum term, and one output NOR gate combines them. Single-literal sum terms follow the same rule as before, with the roles of true and complemented forms mirrored.
| Starting form | Gate pairing | Gate count (all inputs available) |
|---|---|---|
| SOP with k multi-literal terms | NAND-NAND | k + 1 |
| POS with k multi-literal terms | NOR-NOR | k + 1 |
| SOP implemented with NOR gates only | Convert to POS first, or use more levels | Usually more than k + 1 |
Common errors
Other mistakes that GATE questions exploit:
- Forgetting input inverters. The k + 1 count only holds if complemented literals are free. Read the problem for "only true inputs available" or "complemented inputs also available".
- Counting the output of NAND-NAND as a plain OR. The second-level NAND is an OR of complemented products. It only works because the first level has already complemented each product.
- Mixing gate types. A NAND-NAND circuit realizes SOP. A NOR-NOR circuit realizes POS. If the minimal form you found is SOP and the question requires NOR-only, convert the function to its minimal POS form first, then compare costs.
- Using the wrong XOR count. Four gates is correct for XOR in NAND and for XNOR in NOR. The other pairing needs 5.
Quick procedure
- Minimize the function with a K-map, in SOP for NAND or POS for NOR.
- Count one first-level gate per multi-literal term.
- Add one output gate.
- Add inverters for complemented literals that must be fed into first-level gates, and for uncomplemented single-literal terms.
- If the question restricts fan-in to 2, expand wider gates into trees of 2-input gates. Each wider NAND needs an AND tree followed by one inversion, so check the count carefully.
Exercise
Show that the minimum number of 2-input NAND gates for A XOR B is 4, and give the expression.
Show answer
Let t = NAND(A, B) = (AB)'. Then NAND(A, t) = (A(AB)')' = (A(A' + B'))' = (AB')' and NAND(B, t) = (A'B)'. Output NAND of these two gives AB' + A'B = A XOR B. This uses gates t, NAND(A, t), NAND(B, t) and the output gate, so 4 gates.
Exercise
Implement F = AB + C' using NAND gates only (complemented inputs not available). How many 2-input NAND gates are needed?
Show answer
F = NAND(NAND(A, B), C). The first gate gives (AB)', and the output gate computes ((AB)' · C)' = AB + C'. Since C enters in true form, no inverter is needed. The total is 2 gates.
Exercise
Implement F = A'B + AC with NAND gates only, with only true inputs available. How many 2-input NAND gates are needed?
Show answer
Use one NAND(A, A) to get A', then NAND(A', B), NAND(A, C), and an output NAND of the two products. The total is 4 gates.
Exercise
A function has minimal POS form (A + B')(C + D). Write a NOR-only implementation and count the 2-input NOR gates, given that B' must be generated from B.
Show answer
Write B' as NOR(B, B), costing 1 gate. Then NOR(A, B') gives (A + B')' and NOR(C, D) gives (C + D)'. The output NOR of these two gives (A + B')(C + D). The total is 4 gates.