Skip to content

Logic gates explained, with truth tables

Every digital chip, from a microcontroller to a GPU, is built from a handful of logic functions. Learn the seven basic gates and you can read most combinational logic.

Signals are 0 or 1

A logic gate takes one or more binary inputs and produces one binary output. In a circuit, 0 and 1 are two voltage ranges; in an expression they are just false and true. A truth table lists the output for every possible combination of inputs: two inputs give four rows, three give eight, and n inputs give 2ⁿ rows.

AND, OR and NOT

AND outputs 1 only when every input is 1. OR outputs 1 when at least one input is 1. NOT (an inverter) has one input and flips it.

AND: y = a & b
aby
000
010
100
111
OR: y = a | b
aby
000
011
101
111

Try NOT in the simulator: ~a

NAND and NOR

NAND is AND followed by NOT; NOR is OR followed by NOT. They matter because each is universal: any Boolean function can be built from NAND gates alone, or from NOR gates alone. For example, a NAND with both inputs tied together is an inverter, and two NANDs in a row make an AND.

NAND: y = ~(a & b)
aby
001
011
101
110
NOR: y = ~(a | b)
aby
001
010
100
110

In CMOS, the logic family almost every modern chip uses, the natural single-stage gates are inverting: NAND, NOR and NOT. An AND gate is usually a NAND followed by an inverter.

XOR and XNOR

XOR (exclusive OR) outputs 1 when its inputs differ. XNOR outputs 1 when they are equal, which makes it a one-bit equality check. XOR is the sum bit of a binary adder and the heart of parity and checksum circuits.

XOR: y = a ^ b
aby
000
011
101
110
XNOR: y = a ~^ b
aby
001
010
100
111

See a full-adder sum bit: a ^ b ^ cin

The same gates in Verilog

assign y_and  = a & b;
assign y_or   = a | b;
assign y_not  = ~a;
assign y_nand = ~(a & b);
assign y_nor  = ~(a | b);
assign y_xor  = a ^ b;
assign y_xnor = a ~^ b;
Continuous assignments for each gate, using Verilog's bitwise operators.

Gates with more than two inputs

AND, OR, NAND and NOR extend naturally to three or more inputs: a three-input AND is 1 only when all three inputs are 1, and a three-input NOR is 1 only when all three are 0. Multi-input XOR is different: it outputs 1 when an odd number of inputs are 1, which is exactly a parity check.

Be careful with NAND and NOR chains. A three-input NAND is not the same as two two-input NANDs in a row, because the inversion happens after each gate. The simulator shows the difference: (a NAND b) NAND c and ~(a & b & c) have different truth tables.

See four-input parity: a ^ b ^ c ^ d

From gates to circuits

Combinational circuits are built by wiring gate outputs to other gate inputs. Two classic examples use only the gates above. A half adder adds two bits: the sum is a ^ b and the carry is a & b. A 2-to-1 multiplexer picks one of two inputs with a select line: y = ~s & d0 | s & d1.

  • Full adder sum: a ^ b ^ cin
  • Full adder carry: a & b | cin & (a ^ b)
  • Two-bit equality: (a1 ~^ b1) & (a0 ~^ b0)
  • Majority vote of three: a & b | a & c | b & c

Each of these is a single expression you can paste into the simulator to read its truth table and export it as a Verilog module.

Try the multiplexer: ~s & d0 | s & d1

Common mistakes

  • Reading XOR as OR. They differ only when both inputs are 1: OR gives 1, XOR gives 0.
  • Forgetting that NAND and NOR invert the whole result, not each input.
  • Assuming a gate symbol with a bubble on an input is the same as one with a bubble on the output. By De Morgan's theorem, an AND with inverted inputs is a NOR, not a NAND.

Sources and further reading

Every law, table and example on this page is checked by the automated tests of the simulator’s logic engine.