Uni-Sine
Computer ScienceComputer systems

What is a logic gate?

What does a logic gate do?

Inside a computer, every signal is a voltage that is either high or low. We write high as 1 (true, on) and low as 0 (false, off). A logic gate looks at its input signals and decides whether its output should be 1 or 0. The rule never changes, so the same inputs always give the same output.

On their own, gates make simple decisions. Joined together, they can add numbers, compare values, choose which data to send where and store bits, which is how a CPU works at the lowest level. Our guide to how a CPU works shows what all those gates add up to.

Truth tables

A truth table lists every possible combination of inputs and the output for each. With n inputs there are 2n combinations, so a gate with 2 inputs has 4 rows and a circuit with 3 inputs has 8. Write the inputs in binary counting order (00, 01, 10, 11) so you never miss a row.

The 7 types of logic gates

AND, OR and NOT: the basic gates

  • AND: the output is 1 only when both inputs are 1. Think of two switches in a row: the light is on only if both are closed.
  • OR: the output is 1 if either input, or both, is 1. Think of two switches side by side: either one turns the light on.
  • NOT: one input, and the output is the opposite. 1 becomes 0 and 0 becomes 1. It is also called an inverter.

NAND, NOR, XOR and XNOR

  • NAND (NOT AND): an AND gate followed by a NOT. The output is 0 only when both inputs are 1.
  • NOR (NOT OR): an OR gate followed by a NOT. The output is 1 only when both inputs are 0.
  • XOR (exclusive OR): the output is 1 when the inputs are different. Unlike OR, it gives 0 when both inputs are 1.
  • XNOR: the opposite of XOR. The output is 1 when the inputs are the same.

Logic gate truth tables

Here are all six two input gates side by side. NOT has one input: NOT 0 = 1 and NOT 1 = 0.

ABANDORNANDNORXORXNOR
00001101
01011010
10011010
11110001

Notice each "N" gate is its partner with every output flipped: the NAND column is the AND column upside down, and so on. Wire any of them up and flip the inputs in our Logic Gate Simulator.

Interactive calculatorLogic Gate SimulatorDrag gates onto a canvas, wire them together, and watch the signals flow as you switch the inputs on and off.

Try it: this question gives you a gate and its inputs and asks for the output.

Predict a logic gate's output from its inputs.

Press Mark to have your answer marked in the Circuits course.

Logic gate symbols

Circuit diagrams use a symbol for each gate. Most UK and US textbooks and exams use the "distinctive shape" symbols: AND has a flat back and a round front, like a letter D; OR has a curved back and a pointed front; NOT is a triangle. A small circle on an output means "inverted", which is how NAND, NOR and XNOR are drawn: an AND, OR or XOR shape with a circle on the tip. XOR is an OR shape with an extra curved line across the inputs.

You may also see rectangular symbols from the international IEC standard, each a box with a label inside: & for AND, ≥1 for OR, =1 for XOR and 1 for NOT.

Boolean expressions

A logic circuit can also be written as a Boolean expression, a kind of algebra for 0s and 1s. The AQA GCSE specification uses this notation:

GateSymbol in expressionsExampleRead as
AND.A.BA and B
OR+A+BA or B
NOTA bar over the letterA̅Not A
XOR⊕A⊕BA exclusive or B

Other courses write AND as ∧, OR as ∨ and NOT as ¬, which mean the same thing. The Truth Table Generator builds the full table for any expression you type, and the Boolean Algebra Simplifier shrinks an expression to fewer gates.

Combining logic gates: a worked example

A burglar alarm should sound (Q = 1) if the door sensor A or the window sensor B is triggered, and the alarm is switched on, C. As an expression, that is Q = (A + B).C: an OR gate for the two sensors, feeding an AND gate with C.

Three inputs means 23 = 8 rows. Add a column for the middle step, A + B, then AND it with C:

ABCA + BQ = (A + B).C
00000
00100
01010
01111
10010
10111
11010
11111

The alarm sounds in exactly three cases: whenever it is switched on and at least one sensor is triggered. With C = 0, nothing sets it off, which is what you want when you are home.

How gates add binary numbers: the half adder

Adding two single bits gives a sum bit and a carry bit. Compare that with the truth tables above: the sum is 1 only when the two bits differ, which is XOR, and the carry is 1 only when both are 1, which is AND. So one XOR gate and one AND gate make a half adder.

ABSum (A XOR B)Carry (A AND B)In binary
00000 + 0 = 00
01100 + 1 = 01
10101 + 0 = 01
11011 + 1 = 10

Chain adders together, passing each carry to the next, and you can add numbers of any length. Half adders and full adders are on the AQA A-level specification, and the Circuits course builds one gate by gate.

Try one: this question gives you a small circuit of gates and asks you to trace the output.

Trace a two-gate circuit to its final output.

Press Mark to have your answer marked in the Circuits course.

How logic gates are made

Modern gates are made of transistors, tiny switches controlled by voltage. In the CMOS technology used in almost all chips, a NOT gate takes 2 transistors and a two input NAND gate takes 4. NAND and NOR are called universal gates because any other gate, and so any digital circuit at all, can be built from just one of them. For example, a NAND gate with both inputs joined acts as a NOT gate.

Where logic gates are used

  • Processors: the ALU that adds and compares numbers is a large network of gates.
  • Memory: pairs of gates wired in a loop, called flip-flops, can hold a single bit.
  • Everyday control systems: a car's warning chime (door open AND key in), a lift that only moves when the doors are shut, a washing machine that won't spin with the door open.
  • Error checking: XOR gates compute parity bits that spot when data has been corrupted.

Common mistakes

  • Mixing up OR and XOR. OR gives 1 when both inputs are 1; XOR gives 0.
  • Missing rows in a truth table. Three inputs need 8 rows. Count up in binary to get them all.
  • Skipping the middle columns. For a combined circuit, add a column for each gate's output before the final one.
  • Reading A.B as multiplication of numbers. In Boolean algebra the dot means AND, though for 0s and 1s it does give the same answer.
  • Forgetting the circle. The small circle on a symbol's output is what turns AND into NAND.
  • Thinking NAND means "neither". NAND is 1 unless both inputs are 1. "Neither" is NOR.

Common questions

What are the 7 basic logic gates?
AND, OR, NOT, NAND, NOR, XOR and XNOR. AND, OR and NOT are the basic three; the others are combinations of them.
What is a logic gate made of?
Transistors. In modern chips, a NOT gate uses 2 transistors and a two input NAND gate uses 4, all switching on and off billions of times a second.
What is the difference between OR and XOR?
Both output 1 when exactly one input is 1. When both inputs are 1, OR outputs 1 but XOR outputs 0. XOR means one or the other, but not both.
Why are NAND and NOR called universal gates?
Because you can build every other gate, and so any digital circuit, from NAND gates alone or from NOR gates alone.
How many rows does a truth table have?
2 to the power of the number of inputs: 2 rows for one input, 4 for two, 8 for three and 16 for four.
What is a logic gate in simple terms?
A tiny decision maker. It looks at one or more on or off signals and turns its output on or off by a fixed rule, such as "on only if both inputs are on".

Revision cards

Read the front, say your answer, then flip to check.

Where to go next

  • Solve it

    Logic Gate Simulator

    Wire up logic gates and watch the signals flow

    Open the tool
  • Learn it

    Circuits

    Resistors, capacitors and logic gates: build and understand real circuits

    Start the course
  • Test it

    GCSE Computer Science

    A new exam-style paper every day, with instant marking and a mark scheme

    Start today's paper
Read next
Computer Science · Data and logicLogic gate simulator: build circuits, see the truth tableBuild logic circuits from AND, OR, NOT, NAND, NOR and XOR gates, flip switches and watch the signals, with a live truth table and an expression per lamp.