Logic Gates
Every computation your laptop performs reduces to switches turning each other on and off. Logic gates are those switches. They are wired to implement the operations of Boolean Algebra in silicon. A single gate seems trivial — an AND gate just multiplies two bits. But stacked by the billions, gates become memories, arithmetic units, and eventually entire processors. Understanding gates bridges two worlds. One is the abstract 0s and 1s of Binary and Hexadecimal Representation. The other is the physical transistors that make computers real. The motivating problem is deceptively simple. How do you build a machine that decides, remembers, and computes? It must do this using only wires and switches that are "on" or "off". The answer was discovered independently in different forms by switching theorists in the 1930s. A tiny handful of primitive functions, wired together in the right patterns, can express any Boolean function whatsoever. That handful is the set of logic gates: AND, OR, NOT, and their derived cousins NAND, NOR, and XOR. This matters far beyond textbooks. Every CPU, GPU, memory chip, and network router is, at its lowest level, a vast fabric of logic gates. These gates are fabricated as transistors on silicon. The design of adders, multiplexers, decoders, and eventually entire instruction pipelines all begins the same way. It starts with the question "what gate do I need here, and how do I compose it with others?" Hardware description languages like Verilog and VHDL ultimately compile down to gate-level netlists. Chip fabrication toolchains ("standard cell libraries") are literally catalogs of pre-built gates. This article gives you the precise definition of each standard gate. It covers its truth table, its schematic symbol, and the notion of universal gates that can build all others. It also explains the reasoning — not just the assertion — for why NAND and NOR suffice to construct every possible Boolean function. It sets up the next steps in digital design: combining gates into adders and multiplexers, and using feedback to build memory.
- A logic gate implements one Boolean function on one or more input bits, producing one output bit.
- The core gates are AND, OR, NOT; derived gates are NAND, NOR, XOR, and XNOR.
- A truth table fully specifies a gate's behavior: every input combination mapped to an output.
- NAND alone (and separately NOR alone) is functionally complete — universal for all Boolean logic.
- XOR is the "difference detector" gate, central to adders and parity/error-checking circuits.
- Real chips build gates from transistors (usually CMOS) and compose gates into arithmetic, memory, and control logic.
Any Boolean function, no matter how complex, can be built from nothing but NAND gates (or nothing but NOR gates) — a handful of primitives generate the entire universe of digital logic.
The idea
A logic gate is a physical or abstract device that computes a single Boolean Algebra function of one or two input bits, producing exactly one output bit. Inputs and outputs take only two values, conventionally written `0` and `1`, and corresponding physically to two voltage ranges (e.g., "low" near 0V and "high" near the supply voltage). Because gates operate on discrete voltage levels rather than continuous ones, small amounts of electrical noise are automatically suppressed — this is the foundational reason digital circuits are reliable where analog ones are fragile. The behavior of a gate is completely and unambiguously specified by its truth table: a table listing every possible combination of input values together with the corresponding output. For input bits there are rows, since each bit independently takes one of 2 values. A gate with 2 inputs has 4 rows; a gate with 3 inputs has 8 rows. Because a truth table is exhaustive — it lists every input combination, not just typical ones — two gates with identical truth tables are functionally indistinguishable no matter how differently they are built internally. This is precisely why we can reason about circuits abstractly (with Boolean algebra) while manufacturers reason about them physically (with transistors), and both descriptions agree. The primitive gates are:
- NOT (inverter): one input, flips it. .
- AND: output is 1 only if all inputs are 1. .
- OR: output is 1 if at least one input is 1. .
From these, three derived gates matter enormously in practice:
- NAND = NOT(AND): output is 0 only if all inputs are 1.
- NOR = NOT(OR): output is 1 only if all inputs are 0.
- XOR (exclusive OR): output is 1 if the inputs differ. .
- XNOR = NOT(XOR): output is 1 if the inputs are equal.
A set of gates is functionally complete (or universal) if, by composing gates from that set alone, you can build a circuit realizing any Boolean function of any number of inputs. AND, OR, and NOT together are trivially complete — this is essentially the statement that every Boolean function has a sum-of-products (disjunctive normal form) expression. The deeper and more useful fact is that NAND by itself is complete, and separately, NOR by itself is complete. This is why NAND and NOR are called universal gates: a fabrication line that can manufacture only one of these two gate types can, in principle, build any digital circuit at all, including every gate in this article.
How it works
Truth tables of the primitive and derived gates
For two inputs :
| A | B | AND | OR | NAND | NOR | XOR | XNOR |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
NOT has a single input:
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
Notice the row-by-row relationships: NAND's column is exactly AND's column with every bit flipped, and NOR's column is exactly OR's column flipped — this is literally what "NOT-AND" and "NOT-OR" mean, and it is verifiable by direct inspection of the table above.
Building any gate from NAND alone
The claim that NAND is universal is not mysterious once you see the constructions. Each of the following can be checked by tracing signals through the small circuit and confirming the resulting truth table matches the target gate exactly.
NOT from NAND. Tie both inputs of a NAND gate to the same signal . Then NAND.
AND from NAND. Feed into a NAND gate to get , then invert that result using a NOT-from-NAND (i.e., another NAND with both inputs tied together): .
OR from NAND. By De Morgan's law, . So invert , invert (each with a NAND-as-NOT), then NAND the two inverted signals together.
XOR from NAND. A standard 4-NAND construction: ``` n1 = NAND(A, B) n2 = NAND(A, n1) n3 = NAND(B, n1) Y = NAND(n2, n3) ``` Tracing all four input rows confirms (worked in detail below). Since NOT, AND, and OR can each be built from NAND, and {AND, OR, NOT} is already complete (every Boolean function has a sum-of-products form built from ANDs, ORs, and NOTs), it follows that NAND alone is complete. The symmetric argument, using , shows NOR alone is also complete.
Algorithm: evaluating any gate network (simulation)
In practice, digital designers verify gate networks by simulation: propagate known input values forward through a directed acyclic graph of gates and confirm the outputs match a specification. This is the algorithmic heart of every hardware simulator, from a classroom truth-table checker to full SPICE-level tools. ```
# Represent a combinational circuit as a DAG of gates.
# Each gate node has: a type (AND, OR, NOT, NAND, NOR, XOR, XNOR),
# a list of input wires, and one output wire. function SIMULATE(circuit, input_assignment): value = {} # wire -> 0/1 for wire, bit in input_assignment.items(): value[wire] = bit order = TOPOLOGICAL_SORT(circuit) # gates ordered so inputs are ready for gate in order: ins = [value[w] for w in gate.inputs] value[gate.output] = APPLY(gate.type, ins) return {w: value[w] for w in circuit.output_wires} function APPLY(gate_type, ins): if gate_type == NOT: return 1 - ins[0] if gate_type == AND: return 1 if all(b == 1 for b in ins) else 0 if gate_type == OR: return 1 if any(b == 1 for b in ins) else 0 if gate_type == NAND: return 1 - APPLY(AND, ins) if gate_type == NOR: return 1 - APPLY(OR, ins) if gate_type == XOR: return ins[0] ^ ins[1] # 2-input case if gate_type == XNOR: return 1 - APPLY(XOR, ins) function TOPOLOGICAL_SORT(circuit):
# circuit must be acyclic (combinational logic has no feedback);
# standard Kahn's-algorithm / DFS topological sort on the gate DAG ... ``` The topological sort is required because a gate's output cannot be computed until every one of its inputs has already been assigned a value — exactly the dependency structure of a DAG.
Complexity & correctness
Time complexity of gate evaluation. A single gate is evaluated in time (bounded by the number of literal inputs, typically 1–3). Simulating an entire circuit with gates and wires takes time: topological sort visits every gate and wire once (standard for DAG algorithms), and each gate is then evaluated once in . This is optimal — you cannot correctly simulate a circuit without at least reading every gate and wire once, so is a matching lower bound.
Space complexity. to store the value of every wire, plus for the gate list itself — linear in the size of the circuit description.
Correctness of gate simulation. The correctness argument rests on an invariant maintained by the topological order: by the time gate is processed, every wire feeding into has already been assigned its correct final value. This holds by construction of a topological sort — a gate is only listed after all gates producing its input wires (or the input wires are primary inputs, given up front). Since combinational logic is required to be acyclic (no feedback path — feedback is what turns gates into memory elements, covered in Latches and Flip-Flops), a valid topological order always exists. By induction on the topological order, every gate computes its output from already-correct inputs, so the whole simulation is correct.
Correctness of the NAND-completeness claim. This is a genuine theorem, not a hand-wave, and it is worth stating the two-step proof precisely. Step 1 — {AND, OR, NOT} is complete. Any Boolean function can be written in disjunctive normal form: for each input row where , form the AND of all variables (complemented where the row has a 0), then OR all such terms together. Every function equals the OR of its "minterms," so AND, OR, and NOT together realize every possible function. This is a direct consequence of the definition of a truth table: it is a listing of the rows where the function is 1, and DNF just encodes that listing as a formula. Step 2 — NAND builds NOT, AND, and OR. Shown explicitly above: NOT = NAND; AND = NOT(NAND); OR = NAND(NOT, NOT) by De Morgan's law . Chaining Steps 1 and 2: since NAND can build NOT, AND, OR, and {NOT, AND, OR} can build any function, NAND alone can build any function. The dual argument with shows NOR is likewise complete.
Honest tradeoffs. Universality is a statement about what is expressible, not about efficiency. A NAND-only implementation of a function may use many more gates, and much more circuit depth (hence propagation delay), than a design using a rich mixture of gate types. Real standard-cell libraries include dozens of gate variants (including multi-input gates and complex "compound" gates like AND-OR-INVERT) precisely because using the "right" gate directly is faster and smaller than always reducing to NAND. Universality guarantees feasibility; it says nothing about optimality.
Worked example
Trace the 4-NAND XOR circuit from the diagram through all four input combinations, not just the one pictured, to confirm it truly implements XOR everywhere. Recall the construction: ``` n1 = NAND(A, B) n2 = NAND(A, n1) n3 = NAND(B, n1) Y = NAND(n2, n3) ```
| A | B | n1 = NAND(A,B) | n2 = NAND(A,n1) | n3 = NAND(B,n1) | Y = NAND(n2,n3) | A⊕B (expected) |
|---|---|---|---|---|---|---|
| 0 | 0 | NAND(0,0)=1 | NAND(0,1)=1 | NAND(0,1)=1 | NAND(1,1)=0 | 0 ✓ |
| 0 | 1 | NAND(0,1)=1 | NAND(0,1)=1 | NAND(1,1)=0 | NAND(1,0)=1 | 1 ✓ |
| 1 | 0 | NAND(1,0)=1 | NAND(1,1)=0 | NAND(0,1)=1 | NAND(0,1)=1 | 1 ✓ |
| 1 | 1 | NAND(1,1)=0 | NAND(1,0)=1 | NAND(1,0)=1 | NAND(1,1)=0 | 0 ✓ |
Every row matches the XOR truth table exactly, so this 4-gate NAND network really is a correct implementation of XOR — not merely correct on the one case drawn in the figure. Note the asymmetric-looking middle rows ( and ) both correctly yield despite following different internal value paths, which is exactly the kind of non-trivial case a hasty check might skip. As a second, smaller trace, verify NOR-based construction of AND using :
| A | B | ¬A | ¬B | ¬A NOR? — first OR(¬A,¬B) | NOT of that = A·B | A AND B (expected) |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | OR(1,1)=1 | NOT(1)=0 | 0 ✓ |
| 0 | 1 | 1 | 0 | OR(1,0)=1 | NOT(1)=0 | 0 ✓ |
| 1 | 0 | 0 | 1 | OR(0,1)=1 | NOT(1)=0 | 0 ✓ |
| 1 | 1 | 0 | 0 | OR(0,0)=0 | NOT(0)=1 | 1 ✓ |
(Here NOT and OR are themselves built from NOR the same way NOT and AND were built from NAND, by symmetry.)
Common pitfalls & traps
| Pitfall | Why it happens | Fix |
|---|---|---|
| Confusing NAND/NOR with "NOT of A, NOT of B" separately | Students bar each input instead of barring the combined AND/OR | NAND = NOT(A AND B), one bar over the whole expression, per De Morgan |
| Treating XOR as "OR but different" without checking the (1,1) row | XOR and OR agree on 3 of 4 rows; only (1,1) differs | Always check the all-1s row explicitly: OR→1, XOR→0 |
| Assuming any single gate type (e.g., AND alone) is universal | Confusing "useful" with "complete" | Only NAND and NOR (among 2-input gates) are individually universal; verify via the NOT/AND/OR construction |
| Building a "circuit" with a feedback loop and calling it combinational | Feedback creates memory (latches), not a pure function of current inputs | Combinational logic must be an acyclic DAG; cyclic designs need timing analysis, covered under Latches and Flip-Flops |
| Reading a truth table's row order incorrectly (assuming MSB/LSB convention without checking) | Different textbooks order rows differently | Always relabel rows explicitly by the actual input bit pattern, don't assume position |
Pioneers & history
The theoretical bridge between Boolean algebra and physical switching circuits was built by several people working on telephone relay networks and symbolic logic in the early-to-mid 20th century.
Origins · 1 minClaude Elwood Shannon and how this was discovered
Shannon's 1937 master's thesis, "A Symbolic Analysis of Relay and Switching Circuits," proved that Boolean algebra could exactly describe the behavior of switching (relay) circuits — the theoretical foundation that makes "logic gate" a meaningful, mathematically precise physical object rather than just a metaphor. He later founded information theory, but this earlier work is arguably the single most consequential master's thesis in the history of computing.
Where this matters
Every digital integrated circuit — from a microcontroller in a toaster to a data-center CPU — is fabricated as a network of logic gates realized in transistors, almost universally using CMOS technology today because of its extremely low static power draw. Hardware description languages (Verilog, VHDL, SystemVerilog) let engineers describe circuits at a high level, but synthesis tools compile that description down into a gate-level netlist drawn from a standard-cell library — literally a catalog of pre-characterized AND, OR, NAND, NOR, XOR, and compound gates, each with known area, speed, and power. Arithmetic Logic Units use XOR gates as the core of binary adders (a full adder is built from XOR and AND/OR gates); parity bits and simple error-detection codes in memory and networking hardware are direct applications of XOR's "detect a difference" property. Field-Programmable Gate Arrays (FPGAs) are literally arrays of reconfigurable lookup tables that can be programmed to emulate any small logic function, echoing the universality principle at a coarser grain. The next layers of digital design — Combinational Logic Design, building Multiplexers and Decoders, and using controlled feedback to build Latches and Flip-Flops — all start from exactly the gates defined here.
Ask the tutor: How do you mechanically convert a truth table into a minimal NAND-only circuit?
Why do real chips use NAND gates so heavily instead of AND/OR directly?
In CMOS technology, a NAND gate is built from just two PMOS and two NMOS transistors and does not require a separate inverting stage the way a "true AND" would (a CMOS AND is actually a NAND followed by an inverter internally). NAND gates are therefore smaller, faster, and lower-power than AND gates at the transistor level. Since NAND is also functionally universal, chip designers often express logic in terms of NAND/NOR-heavy structures during synthesis and layout, even though the high-level design was expressed with AND/OR/XOR for readability. This is a case where a mathematical fact (universality) and an engineering fact (transistor efficiency) reinforce the same practical choice.
Is XOR really necessary, or could you always just use AND/OR/NOT instead?
XOR is not strictly necessary in the sense of expressiveness — {AND, OR, NOT} is already complete, so any function XOR can compute, they can too. But XOR captures "inputs differ" in a single gate, whereas expressing the same thing with AND/OR/NOT requires several gates (e.g., ). Because "detect a difference" or "add without carry" comes up so often in arithmetic and error-checking circuits, XOR is kept as a first-class primitive for efficiency and clarity, not out of mathematical necessity.
What's the difference between a truth table and a Boolean expression for the same gate?
A truth table is an exhaustive, tabular listing of every input-output pair — it is the definition of the function, unambiguous and complete by construction. A Boolean expression (like for XNOR) is one particular formula that happens to produce that same table; many different-looking expressions can produce identical truth tables (these are called equivalent expressions). Truth tables are used to verify correctness exhaustively; Boolean expressions are used to reason algebraically and simplify circuits (e.g., via Boolean algebra laws or Karnaugh maps).
Can a gate have more than two inputs?
Yes. AND, OR, NAND, and NOR generalize naturally to inputs: an -input AND is 1 only if all inputs are 1, an -input OR is 1 if at least one is 1, and so on. XOR also generalizes, computing the parity (odd number of 1s) of its inputs, which is why XOR trees are used for parity-check circuits. NOT is inherently single-input, since "inverting" only makes sense for one bit at a time. In practice, standard-cell libraries offer 2-, 3-, 4-, and sometimes 8-input variants of AND/OR/NAND/NOR because wide gates are sometimes faster than chaining many 2-input gates.
Why is NOR gate universality important given that NAND already suffices?
Having two independent universal families matters for practical fabrication choices and for theoretical robustness. Some transistor technologies or historical logic families (certain relay and vacuum-tube designs, and some CMOS variants) naturally produce NOR-like behavior more efficiently than NAND-like behavior, so having a proof that NOR alone suffices means a fabrication process built around NOR gates loses no expressive power. It is also a nice symmetry check: the NAND proof uses , and the NOR proof uses the dual identity — each is De Morgan's law applied in the opposite direction.
How does this connect to combinational circuits like adders and multiplexers?
Every combinational circuit — an adder, a multiplexer, a decoder, an ALU — is, at its lowest level, nothing more than a specific wiring of the gates described here. A one-bit full adder, for instance, is built from two XOR gates (to compute the sum bit) and AND/OR gates (to compute the carry-out bit). Once you understand truth tables and gate composition, building a multiplexer (choosing one of several inputs via AND/OR/NOT logic controlled by select lines) or a decoder (the reverse operation) is simply a matter of writing down the desired truth table and mechanically deriving an AND-OR (or NAND-NAND) implementation, which is exactly the subject of Combinational Logic Design.
Why does the article say gates suppress noise, and how does that relate to digital vs analog design?
Digital gates interpret a wide range of input voltages as either "0" or "1" using threshold regions with built-in margins (noise margins), and their output stages actively restore the signal to a clean "full 0" or "full 1" voltage regardless of small input distortions. This "signal regeneration" property means small noise, resistive losses, or slight component variation do not accumulate across many gate stages the way they would in an analog amplifier chain. It is the deep reason large-scale digital systems (with billions of gates) remain reliable, while comparably large analog systems would be overwhelmed by accumulated noise and component drift.