Logic Gates & Truth Tables

Beginner · IntermediateAbout 12 minData & binary, Programming & algorithms

Every decision a computer makes — from adding numbers to showing this page — is built from tiny switches that answer yes or no.

Yes or No, 1 or 0

A computer's chips contain billions of tiny electronic switches. Each one is either on or off — 1 or 0. A logic gate takes one or two of those 1s and 0s as inputs and gives a single 1 or 0 back out.

There are only a few kinds, and you already use them in everyday sentences:

  • AND — "You can go out if your homework is done AND your room is tidy." Both must be true.
  • OR — "We'll cancel if it rains OR it snows." At least one must be true.
  • NOT — "If it's NOT raining, we'll walk." It flips the answer.

A logic gate turns inputs of 1 and 0 into a single output of 1 or 0, following one simple rule.

Truth Tables

A truth table lists every possible input and the output you get. With two inputs there are four rows, because each input can be 0 or 1.

A AND B
ABOut
000
010
100
111
A OR B
ABOut
000
011
101
111
NOT A
AOut
01
10

A quick way to remember: AND is only 1 when everything is 1. OR is only 0 when everything is 0.

Try it yourself

AND
Out: 0

The Same Logic, in Code

Programs use exactly the same three words to make decisions. In pseudocode they combine conditions inside an IF:

IF (age ≥ 13 AND hasPermission)
{
  DISPLAY("Account created")
}

IF (NOT loggedIn)
{
  DISPLAY("Please log in")
}

Each condition is either true or false — a 1 or a 0 — and AND, OR and NOT combine them just like the gates above. A long condition is worked out from the inside out, the same way brackets work in math.

From Gates to Computers

One gate can't do much. But wire a few together and you can add two bits. Wire thousands together and you can add big numbers, compare them, and remember them. A modern processor contains billions of gates — and every one is still just answering "1 or 0?"

That's another example of abstraction: nobody designs a phone one gate at a time. Engineers build small parts from gates, bigger parts from those, and so on up.

Check your understanding

5 quick questions. Pick one answer for each — you'll see why straight away.

  1. A door alarm should ring only when the alarm is switched on AND the door is open. Which gate is that?

  2. What does A OR B give when A is 1 and B is 1?

  3. How many rows does the truth table for a gate with 2 inputs have?

  4. In a program, IF (NOT raining) { DISPLAY("Go outside") }. When is "Go outside" displayed?

  5. Which output does A AND (NOT B) give when A = 1 and B = 1?