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.

In shortA gate turns input bits into one output bit by a fixed rule. NAND and NOR are universal: any circuit can be made from only NAND gates.

Theory in plain words

Gate familiesAND, OR, NOT: the basic threeNAND, NOR: universal gates (build everything)XOR, XNOR: difference and equality detectors
Gate families

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

ABANDORNANDNORXORXNOR
00001101
01011010
10011010
11110001

Gate simulator

Try every gateTry it Yourself

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.

Build NOT, AND and OR from NAND onlyTry it Yourself

Compare this table with the AND and OR columns above. They match, and only NAND was used.

Gates in C and Verilog

Same logic in two languages
// 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?

2. Which gate is universal?

3. XOR output is 1 when...

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.