Logic gates and universal gates
The basic gates and their truth tables, why NAND and NOR are universal, building NOT, AND, OR and XOR from NAND only, two-level NAND-NAND and NOR-NOR circuits, gate counting and fan-in.
Why it exists
A logic gate is the smallest physical building block of a digital circuit: it takes one or more bits and produces one bit as a fixed Boolean function of them. Everything in a processor, from an adder to a register, is a network of a few kinds of gates. Two questions follow, and both appear in GATE. First, which small sets of gates are enough to build every function? (NAND alone, and NOR alone.) Second, given a function, what is the cheapest network in a given gate type, measured in gates or levels? Knowing the standard conversions makes these mechanical.
Prerequisites
You need the laws of Boolean algebra, above all De Morgan and the double-complement law.
Precise definitions
Basic gates. Each output depends only on the present inputs (that is what makes a network of gates combinational).
| Gate | Output | 2-input truth table (inputs 00, 01, 10, 11) |
|---|---|---|
| NOT (inverter) | one input: 1 0 | |
| buffer | one input: 0 1 | |
| AND | 0 0 0 1 | |
| OR | 0 1 1 1 | |
| NAND | 1 1 1 0 | |
| NOR | 1 0 0 0 | |
| XOR | 0 1 1 0 | |
| XNOR | 1 0 0 1 |
Symbols. AND and OR are drawn with a flat and a curved back; a small circle (a bubble) on an input or output means complement. NAND is AND with an output bubble; NOR is OR with an output bubble; XOR has an extra curve on the input side of OR. The triangle is the buffer; with a bubble it is the NOT gate.
Multi-input gates. AND, OR, XOR can have inputs because they are associative. A 2-input-gate tree needs gates and levels for an -input AND, OR or XOR. NAND and NOR are not associative: , so a 3-input NAND is not two 2-input NANDs in a row; it equals , which needs an AND-then-invert structure.
Universal gate. A gate is universal if every Boolean function can be built using only that gate (with inputs and constant lines allowed). NAND and NOR are universal. AND, OR, XOR are not.
Constructions from NAND ( denotes NAND):
- NOT: (1 gate).
- AND: (2 gates).
- OR: (3 gates).
- XOR: , , , (4 gates).
Constructions from NOR ( denotes NOR): NOT ; OR ; AND . By duality these mirror the NAND constructions.
Two-level realisations.
- NAND-NAND. Any SOP is implemented by NAND gates in two levels: one NAND per product term (with the literals as inputs) feeding one NAND that produces the OR. Reason: by De Morgan, where is the output of the first-level NAND. For example .
- NOR-NOR. Any POS is implemented by NOR gates in two levels: one NOR per sum term feeding one NOR. For example .
- A single-literal product term needs only an inverter (or the literal itself) as a first-level input.
Bubble pushing. An AND gate with inverted inputs is a NOR gate, and an OR gate with inverted inputs is a NAND gate (De Morgan drawn as a symbol swap). Moving a bubble across a gate and changing the gate type preserves the function; this is the picture behind the NAND-NAND rule.
XOR and XNOR. XOR gives 1 when its inputs differ. An -input XOR gives 1 when an odd number of inputs are 1 (odd parity); XNOR is its complement. Both are associative and commutative.
Cost measures. The gate count (sometimes the literal count, or the number of gate inputs), the number of levels (propagation delay is roughly proportional to the levels), and the fan-in (inputs per gate) and fan-out (gates driven by one output). In static CMOS, a NAND or NOR with inputs uses transistors, while AND and OR need an extra inverter after the NAND or NOR, which is why NAND and NOR are the natural gates.
How it works
Step through the 4-gate XOR built from NAND gates. The columns are the outputs of the gates and the XOR of the inputs; the last two columns are compared. Then step through the table that shows NAND is not associative.
XOR from four NAND gates
Computed exactly, not typed| A | B | N1=(AB)' | N2=(A.N1)' | N3=(B.N1)' | N4=(N2.N3)' | A xor B |
|---|---|---|---|---|---|---|
| 0 | 0 | · | · | · | · | · |
| 0 | 1 | · | · | · | · | · |
| 1 | 0 | · | · | · | · | · |
| 1 | 1 | · | · | · | · | · |
Step 0
Claim: four 2-input NAND gates make an XOR. N1 is the NAND of the inputs, N2 and N3 each NAND one input with N1, and N4 combines them.
NAND is not associative
Computed exactly, not typed| A | B | C | (A NAND B) NAND C | A NAND (B NAND C) |
|---|---|---|---|---|
| 0 | 0 | 0 | · | · |
| 0 | 0 | 1 | · | · |
| 0 | 1 | 0 | · | · |
| 0 | 1 | 1 | · | · |
| 1 | 0 | 0 | · | · |
| 1 | 0 | 1 | · | · |
| 1 | 1 | 0 | · | · |
| 1 | 1 | 1 | · | · |
Step 0
Claim: unlike AND and OR, grouping matters for NAND. The two ways of grouping three inputs give different functions.
Method for converting a circuit to NAND only: first write the SOP (minimal if possible). Replace every AND by a NAND and the final OR by a NAND. Put an inverter on any single-literal term that enters the second level uncomplemented (a literal entering the output NAND needs its complement at the first level). Count the gates: one per product term of two or more literals, one for the output, and one inverter per literal that needs one.
Worked examples
EX 1NOT, AND, OR and XOR from NAND only
Easy#include <stdio.h>
static int nand(int a, int b) { return !(a && b); }
static int not_(int a) { return nand(a, a); }
static int and_(int a, int b) { return not_(nand(a, b)); }
static int or_(int a, int b) { return nand(not_(a), not_(b)); }
static int xor_(int a, int b) {
int n1 = nand(a, b);
return nand(nand(a, n1), nand(b, n1));
}
int main(void) {
printf("A B | NOT A AND OR XOR (built from NAND only)\n");
for (int a = 0; a < 2; a++)
for (int b = 0; b < 2; b++)
printf("%d %d | %d %d %d %d\n", a, b, not_(a), and_(a, b), or_(a, b), xor_(a, b));
int same = 0;
for (int i = 0; i < 16; i++) {
int a = (i >> 3) & 1, b = (i >> 2) & 1, c = (i >> 1) & 1, d = i & 1;
same += nand(nand(a, b), nand(c, d)) == ((a && b) || (c && d));
}
printf("NAND-NAND matches AB+CD on %d of 16 rows\n", same);
int differ = 0;
for (int i = 0; i < 8; i++) {
int a = (i >> 2) & 1, b = (i >> 1) & 1, c = i & 1;
differ += nand(nand(a, b), c) != nand(a, nand(b, c));
}
printf("(A nand B) nand C differs from A nand (B nand C) on %d of 8 rows\n", differ);
return 0;
}A B | NOT A AND OR XOR (built from NAND only)
0 0 | 1 0 0 0
0 1 | 1 0 1 1
1 0 | 0 0 1 1
1 1 | 0 1 1 0
NAND-NAND matches AB+CD on 16 of 16 rows
(A nand B) nand C differs from A nand (B nand C) on 4 of 8 rowsThe columns are the truth tables of NOT, AND, OR and XOR, so the constructions work. Gate counts: NOT 1, AND 2, OR 3, XOR 4.
EX 2Why NAND is universal
EasyNOT and AND are enough to build everything (every SOP uses only AND, OR and NOT, and OR follows from De Morgan as ). NAND gives NOT with , and AND by inverting a NAND, so NAND alone gives NOT, AND and therefore OR. NOR works the same way by duality.
EX 3SOP to NAND-NAND
MediumImplement with NAND gates only.
Step 1. Two product terms of two literals each, so the first level has two NAND gates, and .
Step 2. The output NAND combines them: by De Morgan.
Count. 3 NAND gates, 2 levels (the program confirms the equality on all 16 input rows).
EX 4POS to NOR-NOR
MediumImplement with NOR gates only.
Step 1. First level: and .
Step 2. Output NOR: .
Count. 3 NOR gates, 2 levels.
EX 5SOP with a single-literal term
GATE levelImplement using NAND gates only (inputs available in true form only).
Step 1. NAND-NAND form: . The first level has the term as a NAND gate, , and the term as the complement , which needs an inverter (a NAND with both inputs tied to ).
Step 2. The output NAND of and gives .
Count. 3 gates: one NAND for , one NAND used as an inverter for , one output NAND. (Inputs in both polarities would save the inverter.)
EX 6Minimum number of NAND gates
GATE levelAn exhaustive search over all circuits of 2-input NAND gates on the inputs finds the following minimum counts: AND needs 2, OR needs 3, XOR needs 4 and XNOR needs 5 (XOR followed by one more NAND used as an inverter is optimal; no 4-gate circuit exists). The XOR-from-NAND animation above is the 4-gate circuit.
EX 7Gate and level count for a large gate
GATE levelAn 8-input AND built from 2-input AND gates in a balanced tree uses gates and has levels. A 9-input one uses 8 gates and levels.
Cheat box
Cheat box
- NAND , NOR , XOR , XNOR .
- NAND from NAND: NOT ; AND = 2 gates; OR = 3 gates; XOR = 4 gates; XNOR = 5 gates.
- NOR dual: NOT ; OR = 2 gates; AND = 3 gates.
- SOP NAND-NAND (first level one NAND per product term, output NAND). POS NOR-NOR.
- NAND and NOR are not associative; AND, OR, XOR are.
- -input AND, OR or XOR from 2-input gates: gates, levels.
- -input XOR is 1 when an odd number of inputs are 1.
- Universal gates: NAND and NOR only.
- Bubble: AND with inverted inputs is a NOR; OR with inverted inputs is a NAND.
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
Within this subject
Universality is formalised in functional completeness and duality. The expressions come from minterm and maxterm forms, and the whole design flow from specification to gates is in combinational design. The adder cell in adders and subtractors is two XOR gates and a majority circuit.
PYQs
6 previous year questions from your book. I re-solved each one myself (6 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Which one of the following expressions does NOT represent exclusive NOR of x and y?
- (a)xy+x'y'
- (b)x⊕y'
- (c)x'⊕y
- (d)x'⊕y'
Which one of the following circuits is NOT equivalent to a 2-input XNOR (exclusive NOR) gate?
- (a)
- (b)
- (c)
- (d)X Y (X,Y) 0 0 0 0 1 0 1 0 1 1 1 1 DIGITAL LOGIC
What is the boolean expression for the output of the combinational logic circuit of NOR gates given below?
- (a)(Q + R̅̅̅̅̅̅̅̅̅)
- (b)(P + Q̅̅̅̅̅̅̅̅̅)
- (c)(P + R̅̅̅̅̅̅̅̅̅)
- (d)(P + Q + R̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅)
What is the minimum number of gates required to implement the Boolean function (AB+C) if we have to use only 2-input NOR gates?
- (a)2
- (b)3
- (c)4
- (d)5
The following expression was to be realized using 2-input AND and OR gates. However, during the fabrication all 2-input AND gates were mistakenly substituted by 2-input NAND gates. (a.b).c + (a'.c).d + (b.c).d + a.d . What is the function finally realized?
- (a)1
- (b)a' + b' + c' + d'
- (c)a' + b + c' + d'
- (d)a' + b' + c + d'
Consider the following logic circuit diagram. Which is/are the CORRECT option(s) for the output function F?
- (a)(XY)'
- (b)X' + Y' + XY'
- (c)(XY)' + X' + XY'
- (d)X + Y'
Self-check
Question 1
How many 2-input NAND gates are needed for an OR gate?
Show answerHide
3: two NAND gates used as inverters (one for each input) and one NAND that combines their outputs.
Question 2
What is the output of a 2-input NOR whose inputs are tied together?
Show answerHide
: it is an inverter.
Question 3
How many 2-input gates and how many levels does a 16-input OR need?
Show answerHide
gates and levels.
Question 4
Is OR, XOR a universal set if only the input variables are available, and if a constant 1 line is also available?
Show answerHide
Without a constant line: no. Every function built from OR and XOR of the input variables has , so NOT cannot be made and not every function is reachable. With a constant 1 line: yes, because gives NOT, and OR with NOT is universal. So the answer depends on whether constants are allowed; the definition on this page allows them, so state the convention in an exam answer (needs verification which convention the exam question assumes).
Revision card
One-screen revision
- NAND and NOR are universal. NOT , AND = 2 NAND, OR = 3 NAND, XOR = 4 NAND, XNOR = 5 NAND.
- SOP NAND-NAND, POS NOR-NOR.
- NAND and NOR are not associative. -input AND/OR/XOR = two-input gates, levels.
- Single-literal terms need an inverter in NAND-NAND.
- -input XOR is odd parity.
References
- ·Mano and Ciletti, Digital Design, 5th ed., Chapter 2 Boolean Algebra and Logic Gates (digital logic gates) and Chapter 3 Gate-Level Minimization (NAND and NOR implementation)
Verification
All constructions (NOT, AND, OR, XOR from NAND or NOR), the NAND-NAND and NOR-NOR realisations of all 256 three-variable functions, non-associativity of NAND and NOR, and the minimum 2-input NAND counts (AND 2, OR 3, XOR 4, XNOR 5, found by exhaustive search over circuits) are checked in scripts/enum-checks/dl_forms.py. The truth-table animations are computed by scripts/gen-dl-traces.py and the C program is compiled and run by npm run verify:c. The CMOS transistor counts are standard facts stated from memory and need a textbook check. Textbook chapter titles are from memory; the independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (conversions, signed ranges and flags, IEEE 754 fields and custom formats, Boolean identities and counts, K-map groups, adder carries, BCD correction, flip-flop conversions, counter sequences and T equations, state reduction, Quine-McCluskey rounds and covers, floating-point alignment and rounding); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; The last Self-check question (is {OR, XOR} universal) assumed no constant lines while the definition on the page allows constants (x XOR 1 gives NOT); the question and answer now state both cases. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)