Français

Practical guide · · The Wire & Gate team

Logic gates explained simply: AND, OR, NOT and XOR

A logic gate takes in 0s and 1s and gives back a single 0 or 1, always following the same rule. Four rules are enough to get started. For each one: an everyday example, its symbol, its truth table and a free circuit to play on the site.

First of all: 0 and 1

In digital electronics, a wire has only two states: low voltage (0) or high voltage (1). You can also read 0 as “false” and 1 as “true”. A logic gate takes one or two of these signals as inputs and produces a single output. In the diagrams below, A and B are the inputs and the lamp stands for the output: on for 1, off for 0. Next to each diagram, the truth table gives the output S (our label, from the French sortie) for every possible combination of inputs.

AND: everything has to be true

The AND gate outputs 1 only when all its inputs are 1. A washing machine only starts if the door is shut AND you have pressed “Start”. With the door open, nothing happens, however hard you press. A card payment goes through if the PIN is right AND there is enough money in the account.

ABAND
ABS
000
010
100
111
The AND gate: only one row out of four lights the lamp, the one where A and B are both 1.

The symbol is D-shaped: the inputs come in on the flat side and the output leaves from the rounded side. Try it: “The double lock”, the circuit on the AND gate page, can be played straight away, with no sign-up.

OR: one is enough

The OR gate outputs 1 as soon as at least one input is 1. A car's interior light comes on if the driver's door OR the passenger's door is open; if both are open, it stays on. That's the catch: in everyday speech, “or” often means “one or the other, but not both”. In logic, OR is happy with both.

ABOR
ABS
000
011
101
111
The OR gate: only the row where A and B are both 0 leaves the lamp off.

The symbol looks like an arrowhead with curved sides, curved inward on the input side. Try it: “The alarm”, on the OR gate page.

NOT: the opposite

The NOT gate (or inverter) has a single input and gives back the opposite: 0 becomes 1, 1 becomes 0. A car's seatbelt warning light works this way: it comes on when the belt is NOT fastened. In programming, it's the “not” in “if the file does not exist”, a test that comes up everywhere.

ANOT
AS
01
10
The NOT gate: just two rows, since there is only one input.

The symbol is a triangle followed by a small circle. The circle means “inverted”, and you'll see it on other gates too. Try it: “The key and the alarm”, on the NOT gate page, which pairs it with an AND.

XOR: one or the other, not both

The XOR gate (exclusive OR) outputs 1 when its two inputs are different. The classic example is a staircase light with one switch at the bottom and one at the top: flip either one and the lamp changes state. Another one: two pupils answer a true-or-false question, and the teacher wants to spot the papers where they disagree. XOR is also how computers add: the sum of two bits, leaving out the carry, is exactly an XOR.

ABXOR
ABS
000
011
101
110
The XOR gate: the lamp lights up on the two rows where A and B are different.

The symbol is the OR shape with a second curved line in front of the inputs. Try it: “The two-way switch”, on the XOR gate page.

What next?

These four gates can be chained: the output of one becomes the input of the next. That is how you get circuits that take a majority vote or add numbers. The 10 free circuits on our home page take you along that path: they open one after the other, bring in one gate at a time, then combine them up to a half adder. Two more gates, NAND and NOR, are simply an AND and an OR followed by an inverter; they have their own page.

The other guides

Articles from the blog