// DIGITAL & ARDUINO · 3 MIN READ

Logic Gates Explained: AND, OR, NOT, NAND, NOR, XOR (Truth Tables)

The 7 basic logic gates — AND, OR, NOT, NAND, NOR, XOR, XNOR — with truth tables, everyday examples and a free logic gate simulator.

ON THIS PAGE
  1. Free logic gate simulator
  2. ON and OFF: 1 and 0
  3. The 7 basic logic gates
  4. AND gate
  5. OR gate
  6. NOT gate (inverter)
  7. NAND gate
  8. NOR gate
  9. XOR gate (exclusive OR)
  10. XNOR gate
  11. All gates in one table
  12. How gates are made
  13. Gates in code
  14. A half adder: gates that do maths
  15. Frequently asked questions

Every phone, laptop and game console is built from billions of tiny decisions. Each decision is made by a logic gate: a little circuit that looks at one or two ON/OFF inputs and decides if its output is ON or OFF. Learn seven gates and you know the alphabet of every computer.

Free logic gate simulator

Flip the two switches and watch each gate decide whether the lamp lights. The highlighted row of the truth table is your switches right now.

⚡ LOADING THE CALCULATOR…

Free, no sign-up. Prefer a bigger screen? Open the Logic gates calculator in the Lab →

ON and OFF: 1 and 0

In digital electronics, a wire is either HIGH (1) — near the supply voltage, like 5 V or 3.3 V — or LOW (0), near 0 V. A truth table lists the output for every possible combination of inputs. With two inputs there are only four rows: 00, 01, 10, 11.

The 7 basic logic gates

AND gate

Output is 1 only when both inputs are 1. Everyday example: a machine that starts only when both safety buttons are pressed.

ABA AND B
000
010
100
111

OR gate

Output is 1 when A or B (or both) is 1. Example: a car's interior light turns on if any door is open.

ABA OR B
000
011
101
111

NOT gate (inverter)

One input, and the output is the opposite. 1 → 0, 0 → 1. Example: a night light — not daylight → lamp on.

NAND gate

AND, then NOT. Output is 0 only when both inputs are 1. NAND is a universal gate: you can build every other gate (and a whole computer) from NAND gates alone. Flash memory is even named after it (NAND flash).

ABA NAND B
001
011
101
110

NOR gate

OR, then NOT. Output is 1 only when both inputs are 0. Also universal.

XOR gate (exclusive OR)

Output is 1 when the inputs are different. Example: a staircase light with a switch at the top and the bottom — flipping either switch changes the light.

ABA XOR B
000
011
101
110

XNOR gate

XOR flipped: output is 1 when the inputs are the same. It is an "equality checker".

All gates in one table

ABANDORNANDNORXORXNOR
00001101
01011010
10011010
11110001

How gates are made

Inside a chip, each gate is a few transistors used as switches. A CMOS NAND gate uses just four. Put billions together and you get adders, memory and a whole processor. You can also buy gates as small chips: the 74HC08 (four AND gates), 74HC00 (four NAND), 74HC86 (four XOR).

Gates in code

You use logic gates every time you write an if:

if (buttonA && buttonB)  { }   // AND
if (doorOpen || keyIn)   { }   // OR
if (!isDay)              { }   // NOT
x = a ^ b;                     // XOR on bits

A half adder: gates that do maths

Add two bits with just two gates: XOR gives the sum bit and AND gives the carry. 1 + 1 → XOR = 0, AND = 1 → binary 10 = 2. Chain these and a computer can add any numbers — see binary and hex.

Frequently asked questions

What are the basic logic gates?

AND, OR and NOT are the three basic gates. NAND, NOR, XOR and XNOR are built from them, giving seven common gates.

What is the difference between OR and XOR?

OR is 1 when at least one input is 1 (including both). XOR is 1 only when exactly one input is 1 — when the inputs are different.

Why is NAND called a universal gate?

Because any other gate — and therefore any digital circuit — can be built using only NAND gates.

What is a truth table?

A table listing the output of a logic circuit for every possible combination of inputs.

Md Kaif · @iam.yourprofessor

Makes 30-second electronics lessons for total beginners — 231+ lessons, free video courses and interactive calculators on this site. Every guide is checked against real formulas and datasheets.

// WATCH · 30 SECONDS EACH

Watch it explained in 30 seconds

All lessons →
// FREE VIDEO COURSE

Go deeper: free 3-minute video lessons

All free courses →
// KEEP LEARNING

More free guides

All guides →