G27
Self-checkedTier 1 12 min readdl.logic-gates-universal

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).

GateOutput2-input truth table (inputs 00, 01, 10, 11)
NOT (inverter)A′A'one input: 1 0
bufferAAone input: 0 1
ANDABAB0 0 0 1
ORA+BA + B0 1 1 1
NAND(AB)′(AB)'1 1 1 0
NOR(A+B)′(A + B)'1 0 0 0
XORA⊕BA \oplus B0 1 1 0
XNOR(A⊕B)′(A \oplus B)'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 nn inputs because they are associative. A 2-input-gate tree needs n−1n - 1 gates and ⌈log⁡2n⌉\lceil \log_2 n \rceil levels for an nn-input AND, OR or XOR. NAND and NOR are not associative: (A↑B)↑C≠A↑(B↑C)(A \uparrow B) \uparrow C \ne A \uparrow (B \uparrow C), so a 3-input NAND is not two 2-input NANDs in a row; it equals (ABC)′(ABC)', 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 (↑\uparrow denotes NAND):

  • NOT: A′=A↑AA' = A \uparrow A (1 gate).
  • AND: AB=(A↑B)↑(A↑B)AB = (A \uparrow B) \uparrow (A \uparrow B) (2 gates).
  • OR: A+B=(A↑A)↑(B↑B)A + B = (A \uparrow A) \uparrow (B \uparrow B) (3 gates).
  • XOR: N1=A↑BN_1 = A \uparrow B, N2=A↑N1N_2 = A \uparrow N_1, N3=B↑N1N_3 = B \uparrow N_1, A⊕B=N2↑N3A \oplus B = N_2 \uparrow N_3 (4 gates).

Constructions from NOR (↓\downarrow denotes NOR): NOT A′=A↓AA' = A \downarrow A; OR A+B=(A↓B)↓(A↓B)A + B = (A \downarrow B) \downarrow (A \downarrow B); AND AB=(A↓A)↓(B↓B)AB = (A \downarrow A) \downarrow (B \downarrow B). 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: ((P1)′(P2)′⋯ )′=P1+P2+⋯((P_1)'(P_2)'\cdots)' = P_1 + P_2 + \cdots by De Morgan, where (Pi)′(P_i)' is the output of the first-level NAND. For example AB+CD=((AB)′(CD)′)′AB + CD = ((AB)'(CD)')'.
  • NOR-NOR. Any POS is implemented by NOR gates in two levels: one NOR per sum term feeding one NOR. For example (A+B)(C+D)=((A+B)′+(C+D)′)′(A+B)(C+D) = ((A+B)' + (C+D)')'.
  • 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 nn-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 nn inputs uses 2n2n 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 N1,N2,N3,N4N_1, N_2, N_3, N_4 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
ABN1=(AB)'N2=(A.N1)'N3=(B.N1)'N4=(N2.N3)'A xor B
00·····
01·····
10·····
11·····

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.

0 / 5
computed by script: exact integer, Fraction and truth-table evaluation (IEEE 754 patterns via Python struct where used), not typed by hand. python3 scripts/gen-dl-traces.py

NAND is not associative

Computed exactly, not typed
ABC(A NAND B) NAND CA NAND (B NAND C)
000··
001··
010··
011··
100··
101··
110··
111··

Step 0

Claim: unlike AND and OR, grouping matters for NAND. The two ways of grouping three inputs give different functions.

0 / 9
computed by script: exact integer, Fraction and truth-table evaluation (IEEE 754 patterns via Python struct where used), not typed by hand. python3 scripts/gen-dl-traces.py

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 xx entering the output NAND needs its complement x′x' 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 rows

The 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

Easy

NOT and AND are enough to build everything (every SOP uses only AND, OR and NOT, and OR follows from De Morgan as A+B=(A′B′)′A + B = (A'B')'). NAND gives NOT with A↑AA \uparrow A, 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

Medium

Implement F=AB+CDF = AB + CD with NAND gates only.

Step 1. Two product terms of two literals each, so the first level has two NAND gates, N1=(AB)′N_1 = (AB)' and N2=(CD)′N_2 = (CD)'.

Step 2. The output NAND combines them: F=(N1N2)′=AB+CDF = (N_1 N_2)' = AB + CD by De Morgan.

Count. 3 NAND gates, 2 levels (the program confirms the equality on all 16 input rows).

EX 4POS to NOR-NOR

Medium

Implement F=(A+B)(C+D)F = (A + B)(C + D) with NOR gates only.

Step 1. First level: N1=(A+B)′N_1 = (A + B)' and N2=(C+D)′N_2 = (C + D)'.

Step 2. Output NOR: (N1+N2)′=N1′N2′=(A+B)(C+D)(N_1 + N_2)' = N_1' N_2' = (A+B)(C+D).

Count. 3 NOR gates, 2 levels.

EX 5SOP with a single-literal term

GATE level

Implement F=A+BCF = A + BC using NAND gates only (inputs available in true form only).

Step 1. NAND-NAND form: F=((A)′(BC)′)′F = ((A)' (BC)')'. The first level has the term BCBC as a NAND gate, (BC)′(BC)', and the term AA as the complement A′A', which needs an inverter (a NAND with both inputs tied to AA).

Step 2. The output NAND of A′A' and (BC)′(BC)' gives A+BCA + BC.

Count. 3 gates: one NAND for BCBC, one NAND used as an inverter for AA, one output NAND. (Inputs in both polarities would save the inverter.)

EX 6Minimum number of NAND gates

GATE level

An exhaustive search over all circuits of 2-input NAND gates on the inputs A,BA, B 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 level

An 8-input AND built from 2-input AND gates in a balanced tree uses 8−1=78 - 1 = 7 gates and has ⌈log⁡28⌉=3\lceil \log_2 8 \rceil = 3 levels. A 9-input one uses 8 gates and ⌈log⁡29⌉=4\lceil \log_2 9 \rceil = 4 levels.

Cheat box

Cheat box

  • NAND =(AB)′= (AB)', NOR =(A+B)′= (A+B)', XOR =A′B+AB′= A'B + AB', XNOR =AB+A′B′= AB + A'B'.
  • NAND from NAND: NOT =A↑A= A \uparrow A; AND = 2 gates; OR = 3 gates; XOR = 4 gates; XNOR = 5 gates.
  • NOR dual: NOT =A↓A= A \downarrow A; OR = 2 gates; AND = 3 gates.
  • SOP →\to NAND-NAND (first level one NAND per product term, output NAND). POS →\to NOR-NOR.
  • NAND and NOR are not associative; AND, OR, XOR are.
  • nn-input AND, OR or XOR from 2-input gates: n−1n - 1 gates, ⌈log⁡2n⌉\lceil \log_2 n \rceil levels.
  • nn-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

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

GATE 2013 · MCQ · 1 markQ18 · page 166 of the book
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'
GATE 2011 · MCQ · 1 markQ20 · page 166 of the book
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
GATE 2010 · MCQ · 1 markQ21 · page 167 of the book
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̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅)
GATE 2009 · MCQ · 1 markQ22 · page 167 of the book
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
GATE 2007 · MCQ · 1 markQ27 · page 168 of the book
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'
GATE 2025 · Set 2 · MSQ · 2 marksQ2 · page 174 of the book
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 answer

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 answer

(A+A)′=A′(A + A)' = A': it is an inverter.

Question 3

How many 2-input gates and how many levels does a 16-input OR need?

Show answer

16−1=1516 - 1 = 15 gates and ⌈log⁡216⌉=4\lceil \log_2 16 \rceil = 4 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 answer

Without a constant line: no. Every function built from OR and XOR of the input variables has F(0,…,0)=0F(0,\dots,0) = 0, so NOT cannot be made and not every function is reachable. With a constant 1 line: yes, because x⊕1=x′x \oplus 1 = x' 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 =A↑A= A \uparrow A, AND = 2 NAND, OR = 3 NAND, XOR = 4 NAND, XNOR = 5 NAND.
  • SOP →\to NAND-NAND, POS →\to NOR-NOR.
  • NAND and NOR are not associative. nn-input AND/OR/XOR = n−1n-1 two-input gates, ⌈log⁡2n⌉\lceil \log_2 n \rceil levels.
  • Single-literal terms need an inverter in NAND-NAND.
  • nn-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