Home / Learn Chip Programming / Logic Gates: AND, OR, NOT, NAND, NOR, XOR
Logic Gates: AND, OR, NOT, NAND, NOR, XOR
Gates are the atoms of digital logic. Every CPU, memory and FPGA is built from them.
Theory in plain words
What it is
A logic gate is a small circuit with one or two inputs and one output. The output is 0 or 1 and depends only on the inputs.
Why we use it
All digital decisions are made of gates: add numbers, compare, select, store. Knowing gates lets you read schematics and Verilog and understand what the CPU does.
How it works
Each gate is made from a few transistors. You describe a gate with a truth table that lists the output for every input combination.
Where it is used
- Inside every ALU, decoder and multiplexer
- Glue logic between chips on a PCB
- Verilog and VHDL designs on FPGA
Truth tables
| 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: output is the opposite of the input.
- AND: 1 only if both inputs are 1.
- OR: 1 if at least one input is 1.
- XOR: 1 if the inputs are different.
- NAND = NOT(AND), NOR = NOT(OR), XNOR = NOT(XOR).
Gate simulator
The current input row is highlighted in the truth table.
Why NAND is universal
NOT A = A NAND A. A AND B = NOT(A NAND B). A OR B = (NOT A) NAND (NOT B). Chip makers love this because one gate type is enough to build anything.
Compare this table with the AND and OR columns above. They match, and only NAND was used.
Gates in C and Verilog
// C (bitwise operators work on every bit at once)
uint8_t y = (a & b) | (~c & d);
// Verilog (describes hardware, all lines exist at the same time)
assign y = (a & b) | (~c & d);Quick quiz
1. Which gate gives 1 only when both inputs are 1?
That is AND.
2. Which gate is universal?
NAND (and NOR) can build every other gate.
3. XOR output is 1 when...
XOR detects a difference.
Common questions
Which gate gives 1 only when both inputs are 1?
AND. That is AND.
Which gate is universal?
NAND. NAND (and NOR) can build every other gate.