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.
| a | b | y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
| a | b | y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
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.
| a | b | y |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| a | b | y |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
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.
| a | b | y |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| a | b | y |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
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;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.
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.
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.