Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 57 · Logic Gates

AND, OR and NOT Gates

Learn the AND, OR and NOT logic gates: symbols, truth tables, Boolean notation, multi-input gates and switch analogies, with a live Python truth-table generator.

A logic gate is the smallest decision-making part of a digital circuit. It takes one or more input bits, applies a fixed rule, and produces one output bit. Every processor, however complex, is made of enormous numbers of three basic building blocks: AND, OR and NOT. Once you understand these three, the rest of this course is about arranging them.

In this lesson you learn what each gate does, how to write it as a symbol, a truth table and an expression, how gates with more than two inputs behave, and how to picture each gate as a simple switch circuit. You will also write a small Python program that prints the truth table of any gate.

The truth table

A gate has no memory and no hidden state: its output depends only on its current inputs. That means its complete behavior can be listed in a truth table, one row for every possible input combination. With n inputs there are 2ⁿ rows, so a two-input gate has four rows and a three-input gate has eight.

Rows are normally listed in binary counting order (00, 01, 10, 11), which makes tables easy to compare.

The three basic gates

AND Output is 1 only if all inputs are 1.
OR Output is 1 if any input is 1.
NOT Output is the opposite of the input.

Every digital circuit can be built from these three.

1 / 2

The AND gate

The AND gate outputs 1 only when all of its inputs are 1. In Boolean algebra AND is written as a dot or simply by placing the letters side by side: Y = A·B or Y = AB. It behaves like multiplication of single bits, which is why it is also called the logical product.

A B Y = A·B
0 0 0
0 1 0
1 0 0
1 1 1

The standard (ANSI/IEEE) symbol has a flat back and a rounded front, like a capital D lying on its flat side, with the inputs entering at the flat edge. A useful way to remember: AND means "A and B must both be present".

The OR gate

The OR gate outputs 1 when at least one input is 1. It is written with a plus sign: Y = A + B. This is the inclusive OR: it is also 1 when both inputs are 1. The plus sign is not ordinary addition, and in Boolean algebra 1 + 1 = 1.

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

The OR symbol has a curved back and a pointed front, like a shield. It is also called the logical sum.

The NOT gate

The NOT gate, also called an inverter, has a single input and outputs the opposite value. It is written with a bar over the variable or a prime: Y = A̅ or Y = A′.

A Y = A′
0 1
1 0

Its symbol is a triangle pointing right, followed by a small circle called a bubble. The bubble is the universal mark for inversion, and you will see it on many other gates. A related part is the buffer: a triangle without a bubble that outputs its input unchanged. A buffer does no logic, but it restores a weakened signal and can drive many loads.

Generate any truth table in Python

You can describe a gate as a Python function and then let itertools.product produce all input combinations in binary counting order. This small tool will be useful throughout the course.

Python A truth-table generator

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last table shows that gates combine just like functions: the output of one gate is the input of the next. A drawing of such a combination is called a logic diagram, and the list of gates and connections is a netlist. You will build a simulator for netlists in a project later in this course.

Gates with more than two inputs

AND and OR are associative: (A·B)·C is the same as A·(B·C), so three or four inputs can be handled by one gate with more input pins, or by a cascade of two-input gates. Real parts often come in two-, three- and four-input versions.

  • A multi-input AND is 1 only for the single row where every input is 1.
  • A multi-input OR is 0 only for the single row where every input is 0.

So with n inputs, an AND is true in exactly 1 of 2ⁿ rows and an OR in 2ⁿ − 1 rows. The NOT gate cannot have more than one input; negating several signals needs several inverters.

Python How many rows are 1?

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The table makes the pattern plain: AND gets rarer and OR more common as inputs are added. This is also why AND is often used as a "detector" for one particular input pattern, an idea that returns in the lessons on decoders and minterms.

Switch analogies

Gates can be pictured with ordinary switches and a lamp, which is how the first relay-based computers actually worked.

  • AND is a series circuit. Two switches in a row: the lamp lights only if both are closed.
  • OR is a parallel circuit. Two switches side by side: the lamp lights if either (or both) is closed.
  • NOT is a normally closed switch. The lamp is on while the switch is not pressed, and pressing it turns the lamp off.

A transistor takes the place of the switch in a real chip, with its input voltage deciding whether the path conducts. The lesson on universal gates shows how a handful of such switches form gates.

Python Switch circuits behave like gates

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first tables are the AND and OR truth tables again, written as lamps. The last circuit is the expression A·(B + C′) built from switches. Reading expressions as circuits (and circuits as expressions) is one of the main skills of digital design.

Order of operations

Boolean expressions follow a precedence similar to arithmetic: NOT first, then AND, then OR. So A + B·C means A + (B·C), and A·B′ means A·(B′), not (A·B)′. Use parentheses whenever you are unsure, and note that a bar over a whole group, as in (A·B)′, is a different expression from putting a bar on each variable.

Everyday applications of gates

Truth tables become less abstract when you see what the gates are for. Almost every control circuit, from a door alarm to a washing machine, is a few gates that say "do this when these conditions hold". Here are five patterns that you will use again and again.

1. Alarms: AND with OR. An alarm should sound when it is armed and any of its sensors fires, unless someone has silenced it. In words: armed AND (door OR window OR motion) AND NOT silenced. The OR gathers the sensors, the AND combines the result with the arming switch, and the NOT gives a silence button priority.

2. Enable and inhibit: a gate as a valve. An AND gate with one input enable passes the other input only while enable = 1, and outputs 0 otherwise. It is a digital valve. Add a NOT and you get an inhibit: data AND enable AND NOT inhibit passes the data only while it is enabled and not blocked. The same idea sits inside every register (the load-enable signal) and every bus (tri-state buffers).

3. Safety interlocks. A press must start only if every safety condition holds: both hands are on the two start buttons (so neither hand can be in the machine), the guard is closed, and the emergency stop is not pressed. That is a single AND with one inverted input. Safety circuits are designed so that any failure to detect a condition gives a 0 and therefore stops the machine.

4. Control from two places: XOR. A staircase light with a switch at the top and at the bottom must change state whenever either switch is flipped. The lamp is top XOR bottom: it is on when exactly one switch is up. (Real house wiring achieves the same effect with two-way switches.)

5. Voting: majority gates. In a system where a wrong answer is dangerous, three independent sensors are read and the output is the majority: AB + BC + AC. A single faulty sensor is outvoted by the other two, which is the principle of triple modular redundancy used in aircraft and spacecraft.

The program checks each pattern against its specification by running it.

Python Alarm, enable gate, interlock, XOR and voter

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the first block. The alarm stays silent when there is no detection (armed but no sensor), also when the system is not armed even if a door opens, and when it is silenced, even if all the sensors fire at once. The output list for the enable gate shows data bits only in the positions where enable is 1, and the inhibit signal cuts two more positions out. In the interlock, exactly one of the sixteen input combinations lets the press run. The staircase table is simply the XOR truth table, and the voter prints True, meaning it returned the right value for every single-sensor failure.

Notice what these examples have in common: each requirement in plain language was translated directly into an expression with AND, OR and NOT. That translation, from words to a Boolean expression, is the first step of every design in this tutorial. The next lessons show how to simplify the expression, and how to combine such functions into larger building blocks.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. For which input row does a 3-input AND gate output 1?

    AND needs every input to be 1, so exactly one of the eight rows (111) gives 1.
  2. 2. What is the output of a 2-input OR gate when both inputs are 1?

    OR is inclusive: it is 1 when at least one input is 1. In Boolean algebra 1 + 1 = 1.
  3. 3. Which switch arrangement matches an AND gate?

    In series, current flows only if both switches are closed, just as AND outputs 1 only if both inputs are 1.
  4. 4. How many rows does the truth table of a 4-input gate have?

    n inputs give 2^n combinations, and 2^4 = 16.
  5. 5. What does the small bubble at the output of a gate symbol mean?

    A bubble is the standard mark for inversion. A triangle with a bubble is a NOT gate, and a bubble on an AND or OR makes a NAND or NOR.

Summary

  • Everyday control logic is plain gates: alarms (AND with OR), enable and inhibit signals, safety interlocks, XOR for two-place switching and majority voters.
  • A gate maps input bits to one output bit; its whole behavior is its truth table, with 2ⁿ rows for n inputs.
  • AND (A·B) is 1 only when all inputs are 1; OR (A + B) is 1 when any input is 1; NOT (A′) inverts one input.
  • A bubble on a symbol means inversion; a buffer is a triangle without a bubble and passes its input through.
  • AND and OR extend to any number of inputs; a wide gate equals a cascade of two-input gates.
  • Series switches act as AND, parallel switches as OR, and a normally closed switch as NOT.
  • Precedence is NOT, then AND, then OR.

Next: NAND, NOR, XOR and XNOR, the gates built from these three.