Logic Gates

Logic gates are the building blocks of digital circuits. An AND gate outputs 1 only when all of its inputs are 1, an OR gate outputs 1 when at least one input is 1, and a NOT gate flips its input. Wiring gates together builds a circuit whose output is a Boolean expression you can simplify.

Before this lesson: Boolean Algebra, Truth Tables and Logical Equivalence

What is a logic gate?

A logic gate is a small electronic device that takes one or more input signals and produces one output signal. Each signal is either 1 (high voltage, true) or 0 (low voltage, false). A gate does exactly one Boolean operation, so a gate is Boolean algebra built in hardware.

A circuit (or combinational circuit) is a network of gates in which the output wire of one gate can feed the input of another. Its output depends only on its current inputs, so it is fully described by a truth table or by a Boolean expression. We use the notation from Boolean Algebra: x + y for OR, xy for AND and \overline{x} for NOT.

The six common logic gates

GateExpressionOutput is 1 whenOutputs for xy = 00, 01, 10, 11
ANDxyboth inputs are 10, 0, 0, 1
ORx + yat least one input is 10, 1, 1, 1
NOT\overline{x}the input is 0 (one input only)x = 0 gives 1; x = 1 gives 0
NAND\overline{xy}at least one input is 01, 1, 1, 0
NOR\overline{x + y}both inputs are 01, 0, 0, 0
XORx \oplus yexactly one input is 10, 1, 1, 0
The standard symbols for six logic gates, each with inputs x and y on the left and its output on the right. AND has a flat back and a round front, output xy. OR has a curved back and a pointed front, output x + y. NOT is a triangle with a small circle at its tip and one input x, output x-bar. NAND is the AND shape with a small circle at the output, output NOT xy. NOR is the OR shape with a small circle, output NOT (x + y). XOR is the OR shape with an extra curved line behind it, output x XOR y.
A small circle (a “bubble”) on an output means NOT: NAND is AND followed by NOT, and NOR is OR followed by NOT.

NAND and NOR are AND and OR followed by NOT. XOR, the exclusive or, is 1 when the inputs differ. In terms of the three basic operations,

x \oplus y = x\overline{y} + \overline{x}y

How to work with logic gate circuits

From a circuit to an expression

  1. Label each input wire with its variable.
  2. Move from left to right. At each gate, write its output in terms of its inputs, and label the gate’s output wire.
  3. The label on the last wire is the circuit’s output expression. Use parentheses when the output of an OR gate feeds an AND gate.

From a truth table to a circuit

  1. Find every row of the truth table where the output is 1.
  2. For each such row, write a product (an AND) of all the variables, putting a bar over each variable that is 0 in that row. This product is 1 for that row only.
  3. Add (OR) these products. The result is the sum-of-products form, also called disjunctive normal form.
  4. Simplify with the laws of Boolean algebra, then build one gate for each operation that is left.

A simpler equivalent expression gives a circuit with fewer gates, which is cheaper, uses less power and responds faster.

Logic gates examples

Example 1: Read a circuit

In the circuit below, x and y enter an AND gate, z enters a NOT gate, and the two outputs enter an OR gate. Write the circuit’s output, then find it when x = 1, y = 0 and z = 0.

A logic circuit with three inputs on the left: x, y and z. Inputs x and y go into an AND gate whose output wire is labeled xy. Input z goes into a NOT gate whose output wire is labeled z-bar. Both wires go into an OR gate on the right, whose output is labeled xy + z-bar.
Label each wire as you go: the AND gate gives xy, the NOT gate gives z-bar, and the OR gate joins them into xy + z-bar.
xy

Label the AND gate’s output: x AND y.

\overline{z}

Label the NOT gate’s output: NOT z.

xy + \overline{z}

The OR gate joins the two wires; this is the output.

1 \cdot 0 + \overline{0}

Substitute x = 1, y = 0 and z = 0.

0 + \overline{0}

AND: 1 \cdot 0 = 0.

0 + 1

NOT: \overline{0} = 1.

1

OR: 0 + 1 = 1.

Answer: the output is xy + \overline{z}, and it equals 1 for these inputs.

Example 2: Build XOR from basic gates

A circuit sends x and y into an OR gate and into a NAND gate, then sends both outputs into an AND gate. Find its output expression and its truth table. Which gate does it act like?

x + y

Output of the OR gate.

\overline{xy}

Output of the NAND gate.

(x + y)\overline{xy}

The AND gate multiplies the two outputs.

xyx + y\overline{xy}(x + y)\overline{xy}
00010
01111
10111
11100

Answer: the output is (x + y)\overline{xy}. Its outputs 0, 1, 1, 0 match XOR, so this circuit is an XOR gate built from OR, NAND and AND. The OR part says “at least one is 1,” and the NAND part says “not both.”

Example 3: Simplify a circuit

A circuit has output xy + x\overline{y} + \overline{x}y. It uses 2 NOT gates, 3 AND gates and 2 two-input OR gates, 7 gates in all. Simplify the output and find an equivalent circuit with fewer gates.

xy + x\overline{y} + \overline{x}y

Start with the circuit’s output.

x(y + \overline{y}) + \overline{x}y

Factor x out of the first two terms (distributive law).

x \cdot 1 + \overline{x}y

Complement law: y + \overline{y} = 1.

x + \overline{x}y

Identity law: x \cdot 1 = x.

(x + \overline{x})(x + y)

Distribute OR over AND: x + yz = (x + y)(x + z).

1 \cdot (x + y)

Complement law: x + \overline{x} = 1.

x + y

Identity law.

Answer: the output simplifies to x + y, so the 7-gate circuit can be replaced by a single OR gate.

Example 4: NAND is a universal gate

Show that NOT, AND and OR can each be built from NAND gates alone.

\overline{x \cdot x}

NOT: send x into both inputs of a NAND gate.

\overline{x}

Idempotent law: x \cdot x = x.

\overline{\overline{xy}}

AND: send the output of NAND(x, y) through a NAND gate used as NOT.

xy

Double complement law.

\overline{\overline{x}\,\overline{y}}

OR: make \overline{x} and \overline{y} with two NAND-as-NOT gates, then NAND them.

\overline{\overline{x}} + \overline{\overline{y}}

De Morgan’s law: \overline{ab} = \overline{a} + \overline{b}.

x + y

Double complement law.

Answer: NOT uses 1 NAND gate, AND uses 2 and OR uses 3. Because every Boolean expression is built from NOT, AND and OR, every circuit can be built from NAND gates alone. NOR gates are universal in the same way.

Example 5: A majority-vote circuit (word problem)

Three judges each press a button: 1 for yes, 0 for no. A contestant moves on when at least two judges vote yes. Design a simple circuit for this rule, with judge votes x, y and z.

xyzOutput M
0000
0010
0100
0111
1000
1011
1101
1111
M = \overline{x}yz + x\overline{y}z + xy\overline{z} + xyz

One product for each row with output 1: rows 011, 101, 110 and 111.

M = \overline{x}yz + x\overline{y}z + xy\overline{z} + xyz + xyz + xyz

Idempotent law: xyz = xyz + xyz + xyz.

M = (\overline{x}yz + xyz) + (x\overline{y}z + xyz) + (xy\overline{z} + xyz)

Regroup into three pairs (commutative and associative laws).

M = yz(\overline{x} + x) + xz(\overline{y} + y) + xy(\overline{z} + z)

Factor each pair (distributive law).

M = yz \cdot 1 + xz \cdot 1 + xy \cdot 1

Complement law, three times.

M = yz + xz + xy

Identity law.

Answer: M = xy + xz + yz. The circuit needs 3 AND gates (one for each pair of judges) and 2 two-input OR gates, 5 gates in all, instead of the 14 two-input gates (3 NOT, 8 AND and 3 OR) that the unsimplified sum-of-products form would use.

Common mistakes

MistakeExample of the errorFix
Applying the bar of a NAND gate to each input separatelyWriting NAND(x, y) as \overline{x}\,\overline{y}By De Morgan’s law, \overline{xy} = \overline{x} + \overline{y}, which is not \overline{x}\,\overline{y}.
Treating OR like XORSaying an OR gate outputs 0 when both inputs are 1OR gives 1 + 1 = 1; only XOR gives 0 for two 1s.
Using ordinary algebra on repeated inputsWriting an OR gate with x on both inputs as 2xIdempotent law: x + x = x.
Dropping parentheses between gatesAn OR gate’s output x + y feeds an AND gate with z, written x + yzWrite (x + y)z: the whole OR output is one input of the AND gate.
Ignoring a bubbleReading a NOR gate as ORA small circle on the output means NOT: NOR is \overline{x + y}.
Building products from the rows where the output is 0Using row 000 in the majority circuitSum-of-products uses only the rows where the output is 1.

Related ideas

The half adder

Computers add binary digits with gates. Adding two bits x and y gives a sum bit x \oplus y and a carry bit xy. For example, 1 + 1 = 10 in binary: the sum bit is 0 and the carry is 1. This XOR-plus-AND circuit is called a half adder, and chains of these circuits add whole numbers.

Karnaugh maps

A Karnaugh map arranges a truth table in a grid so that rows that differ in one variable sit next to each other. Grouping neighboring 1s shows the same simplifications as the laws used in Examples 3 and 5, and is a common shortcut for circuits with three or four inputs.

Practice problems

  1. Find the output of the AND, OR, NAND, NOR and XOR gates when x = 1 and y = 0.
  2. A circuit sends x and y into an OR gate and then sends the result through a NOT gate. Write its output, and find the only input row that gives 1.
  3. A circuit sends x through a NOT gate, sends \overline{x} and y into an AND gate, and then sends that output and z into an OR gate. Write the output and evaluate it at x = 0, y = 1, z = 0.
  4. Simplify the circuit output \overline{x}(x + y).
  5. Simplify the circuit output xy + x\overline{y}z.
  6. Simplify \overline{\overline{x} + \overline{y}}. Which single gate does this circuit act like?
  7. Show how to build a NOT gate from one NOR gate.
  8. A truth table has output 1 only for the rows (x, y) = (0, 1) and (1, 0). Write its sum-of-products expression and name the gate.
  9. A bank vault opens only when the manager’s key is turned (m = 1) and either the correct code is entered (c = 1) or a valid badge is scanned (b = 1). Write a Boolean expression for the vault circuit, and decide whether it opens when m = 1, c = 0 and b = 1.
  10. A half adder adds two bits with sum bit S = x \oplus y and carry bit C = xy. Find S and C for each of the four input pairs.
Show answers

1.

1 \cdot 0 = 0

AND: both must be 1.

1 + 0 = 1

OR: at least one is 1.

\overline{1 \cdot 0} = \overline{0} = 1

NAND: NOT of the AND output.

\overline{1 + 0} = \overline{1} = 0

NOR: NOT of the OR output.

1 \oplus 0 = 1

XOR: the inputs differ.

Answer: AND 0, OR 1, NAND 1, NOR 0, XOR 1

2.

x + y

Output of the OR gate.

\overline{x + y}

The NOT gate complements it (this is NOR).

\overline{x}\,\overline{y}

De Morgan’s law.

\overline{x} = 1,\ \overline{y} = 1

The product is 1 only when both complements are 1.

x = 0,\ y = 0

So both inputs are 0.

Answer: \overline{x + y}, which is 1 only for x = 0, y = 0

3.

\overline{x}

Output of the NOT gate.

\overline{x}y

Output of the AND gate.

\overline{x}y + z

Output of the OR gate.

\overline{0} \cdot 1 + 0

Substitute x = 0, y = 1, z = 0.

1 \cdot 1 + 0

NOT: \overline{0} = 1.

1 + 0

AND: 1 \cdot 1 = 1.

1

OR.

Answer: \overline{x}y + z, which equals 1

4.

\overline{x}x + \overline{x}y

Distributive law.

0 + \overline{x}y

Complement law: \overline{x}x = 0.

\overline{x}y

Identity law.

Answer: \overline{x}y (one NOT gate and one AND gate)

5.

x(y + \overline{y}z)

Factor out x (distributive law).

x\big((y + \overline{y})(y + z)\big)

Distribute OR over AND.

x\big(1 \cdot (y + z)\big)

Complement law.

x(y + z)

Identity law.

Answer: x(y + z)

6.

\overline{\overline{x}}\;\overline{\overline{y}}

De Morgan’s law: \overline{a + b} = \overline{a}\,\overline{b}.

xy

Double complement law.

Answer: xy, so the circuit acts like an AND gate

7.

\overline{x + x}

Send x into both inputs of a NOR gate.

\overline{x}

Idempotent law: x + x = x.

Answer: a NOR gate with both inputs tied to x is a NOT gate

8.

\overline{x}y

Row (0, 1): bar over x, which is 0.

x\overline{y}

Row (1, 0): bar over y, which is 0.

\overline{x}y + x\overline{y}

OR the two products.

Answer: \overline{x}y + x\overline{y} = x \oplus y, the XOR gate

9.

c + b

Code or badge: an OR gate.

m(c + b)

The key must also be turned: an AND gate.

1 \cdot (0 + 1)

Substitute m = 1, c = 0, b = 1.

1 \cdot 1

OR: 0 + 1 = 1.

1

AND.

Answer: m(c + b); the vault opens

10.

S = 0 \oplus 0 = 0,\ C = 0 \cdot 0 = 0

Inputs 0, 0: 0 + 0 = 0.

S = 0 \oplus 1 = 1,\ C = 0 \cdot 1 = 0

Inputs 0, 1: 0 + 1 = 1.

S = 1 \oplus 0 = 1,\ C = 1 \cdot 0 = 0

Inputs 1, 0: 1 + 0 = 1.

S = 1 \oplus 1 = 0,\ C = 1 \cdot 1 = 1

Inputs 1, 1: 1 + 1 = 10 in binary.

Answer: (S, C) = (0, 0), (1, 0), (1, 0), (0, 1)

Logic gates FAQ

What are the three basic logic gates?

AND, OR and NOT. Every other gate, and every Boolean expression, can be built from these three.

What is the difference between OR and XOR?

OR outputs 1 when at least one input is 1, including when both are. XOR (exclusive or) outputs 1 only when exactly one input is 1, so XOR gives 0 when both inputs are 1.

Why are NAND and NOR called universal gates?

Each one can build NOT, AND and OR by itself, as Example 4 shows for NAND. So any digital circuit can be made from NAND gates alone, or from NOR gates alone.

How do you find the Boolean expression of a logic circuit?

Label each input, then work from left to right, writing each gate’s output in terms of its inputs. The expression on the final wire is the circuit’s output.

Summary

  • AND outputs 1 only when all inputs are 1; OR outputs 1 when at least one is 1; NOT flips its input.
  • NAND and NOR are AND and OR followed by NOT; XOR outputs 1 when its inputs differ.
  • Read a circuit by labeling every wire from left to right; use parentheses when an OR output feeds an AND gate.
  • Build a circuit from a truth table with the sum-of-products form, then simplify it with Boolean algebra laws.
  • A simpler equivalent expression means fewer gates; NAND and NOR are each universal.

Previous lesson: Boolean Algebra

Next lesson: Big-O Notation

Back to the Discrete Math Index