Basic Logic Gates (AND, OR, NOT)

Every digital device, from a calculator to a smartphone processor, is built from tiny circuits called logic gates. A gate takes one or more binary inputs, each 0 or 1, and produces a single binary output. Three gates are the foundation of everything else: AND, OR and NOT. Once you understand these three, you can read and design any digital circuit.

In this lesson you will learn what each gate does, how to describe it with a truth table and a Boolean expression, and how to combine gates into a larger function. You should be comfortable with binary values 0 and 1 from the lesson on number systems.

Learning objectives

After this lesson you will be able to:

  • Describe the behaviour of the AND, OR and NOT gates.
  • Write the truth table and Boolean expression of each gate.
  • Evaluate a Boolean expression that combines several gates.
  • Apply the order of operations: NOT, then AND, then OR.
  • Explain how NAND, NOR and XOR relate to the basic gates.

The AND gate

An AND gate outputs 1 only when all of its inputs are 1. Think of two switches in series: current flows only if both are closed. In Boolean algebra, AND is written as multiplication, so the output is Y = AB (also written A . B).

A B Y = AB
0 0 0
0 1 0
1 0 0
1 1 1

An AND gate can have more than two inputs. A 3-input AND gate outputs 1 in only one case, when A, B and C are all 1.

A B C Y = ABC
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1

The OR gate

An OR gate outputs 1 when at least one input is 1. Think of two switches in parallel: current flows if either one is closed. In Boolean algebra, OR is written as addition, so Y = A + B. Remember that this "+" means OR and not arithmetic addition, so 1 + 1 = 1.

A B Y = A + B
0 0 0
0 1 1
1 0 1
1 1 1

A 3-input OR gate outputs 0 in only one case, when every input is 0. In the other 7 combinations it outputs 1.

The NOT gate

A NOT gate, or inverter, has one input and one output, and it flips the value. The output is written A' (read "A prime" or "NOT A"). Some books write it as A with a bar above it.

A Y = A'
0 1
1 0

Order of operations

When one expression contains several operators, evaluate NOT first, then AND, then OR. Parentheses override this order. So A + BC means "A OR (B AND C)", and it is different from (A + B)C.

For example, with A = 1, B = 0 and C = 0:

  • A + BC = 1 + 0 = 1
  • (A + B)C = 1 . 0 = 0

The two expressions give different answers, so parentheses must be written carefully.

Example 1: evaluating an expression

Build the truth table of F = AB + C'. This uses all three gates: an AND gate for AB, a NOT gate for C' and an OR gate to combine them.

A B C AB C' F = AB + C'
0 0 0 0 1 1
0 0 1 0 0 0
0 1 0 0 1 1
0 1 1 0 0 0
1 0 0 0 1 1
1 0 1 0 0 0
1 1 0 1 1 1
1 1 1 1 0 1

Result: F is 0 only when AB = 0 and C = 1, in rows 2, 4 and 6. Everywhere else F is 1. For instance, with A = 1, B = 0 and C = 1, we get AB = 0 and C' = 0, so F = 0.

Example 2: from NAND, NOR and XOR to the basic gates

Three more gates are used so often that they have their own symbols. Each one can be built from the basic three.

A B NAND NOR XOR
0 0 1 1 0
0 1 1 0 1
1 0 1 0 1
1 1 0 0 0
  • NAND is AND followed by NOT: Y = (AB)'.
  • NOR is OR followed by NOT: Y = (A + B)'.
  • XOR outputs 1 when the inputs are different: Y = A'B + AB'.

Result: an XOR gate needs two NOT gates, two AND gates and one OR gate. NAND and NOR are special because either one alone can build every other gate, which is why chip makers like them.

Example 3: a real-world alarm

A house alarm sounds when the system is armed and either the door or the window is open. Write this as a Boolean expression and a truth table.

Let Armed, Door and Window each be 1 when true. The alarm needs Armed AND (Door OR Window), so Alarm = Armed (Door + Window).

Armed Door Window Alarm = Armed (Door + Window)
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1

Result: the alarm is 1 in three of the eight rows. It never sounds when Armed = 0, whatever the door and window do, and it never sounds when both are closed.

Common mistakes

  • Reading + as addition. In Boolean algebra A + B means OR, and 1 + 1 equals 1.
  • Ignoring the order of operations. AB + C and A(B + C) are different functions.
  • Forgetting that NOT binds tightest. A'B means (NOT A) AND B, not NOT (A AND B).
  • Mixing up AND and OR outputs. AND is 1 only when all inputs are 1. OR is 0 only when all inputs are 0.
  • Counting rows wrongly. A gate with n inputs has 2^n rows in its truth table, so 3 inputs need 8 rows and 4 inputs need 16.

Practice problems

Check your answers with the truth table generator or the Boolean calculator.

  1. Evaluate Y = A'B + C for A = 0, B = 1 and C = 0.

    Answer: Y = 1, because A'B = 1 . 1 = 1.

  2. For a 4-input gate, how many input combinations make an AND gate output 1, and how many make an OR gate output 0? How many rows does the truth table have?

    Answer: One combination for each (1111 for AND, 0000 for OR), in a table with 16 rows.

  3. In the alarm of Example 3, what is the output when Armed = 1, Door = 0 and Window = 1?

    Answer: Alarm = 1 . (0 + 1) = 1, so the alarm sounds.

  4. Evaluate A + BC and (A + B)C for A = 1, B = 0 and C = 0.

    Answer: A + BC = 1 and (A + B)C = 0.

Frequently asked questions

What are the three basic logic gates? AND, OR and NOT. Every other digital function can be built from them, and NAND or NOR can each build all the others on their own.

What is the difference between AND and OR? An AND gate needs all of its inputs to be 1 to output 1. An OR gate outputs 1 when any input is 1.

Why do logic gates only use 0 and 1? Digital circuits treat voltages as either low (0) or high (1). Two clear levels are easy to tell apart and are not upset by small amounts of electrical noise.

To practise further, build the truth table of any expression in the truth table generator.

Next lesson: Truth Tables