University of Wales Trinity Saint David
ACCA4021 - Introduction to Computer Systems

Logic Gates

Overview

Every calculation a computer performs is carried out by tiny electronic switches called logic gates. This worksheet introduces the six basic gates, their circuit symbols and their truth tables, and shows how to describe them with Boolean expressions. It builds on the Binary Numbers worksheet, so make sure you are comfortable with bits and binary place value first. Work through the tasks in order. Most tasks have an interactive diagram or self-checking questions, so try each question yourself before checking.

Learning Objectives

By the end of this lab you will be able to:

Concept

Inside a computer, a bit is stored as a voltage that is either low (0) or high (1). A logic gate is a small circuit, built from a handful of transistors, that takes one or two of these bits as inputs and produces one bit as output, following a fixed rule. Every operation a processor performs, from comparing two values to adding them, is built from gates like these.

The rule for a gate is written as a truth table, which lists every possible combination of inputs alongside the output each one produces. Gates can also be described with Boolean algebra, where a dot means AND, a plus means OR, and a bar over a term means NOT. Each gate also has a standard circuit symbol, so a drawing of a circuit shows exactly how the gates are connected.

Because the output of one gate can feed into the input of another, gates can be chained together into larger circuits. Reading such a circuit is a matter of working out each gate's output in turn, starting from the inputs.

Tasks

Task 1 - The three basic gates: NOT, AND and OR

Every logic gate can be built from three basic ones. In the truth tables below, A and B are inputs and Q is the output.

NOT has one input and flips it. A 0 becomes 1 and a 1 becomes 0. It is also called an inverter.

AQ
01
10

AND outputs 1 only when both inputs are 1. Think of two switches wired one after the other. The lamp lights only if both are on.

ABQ
000
010
100
111

OR outputs 1 when at least one input is 1. Think of two switches wired side by side. Either one can light the lamp.

ABQ
000
011
101
111

Notice that a 2-input truth table always has four rows. Each input can be 0 or 1, so there are 2 x 2 = 4 combinations. Listing them in the order 00, 01, 10, 11 means you are counting from 0 to 3 in binary, which is an easy way to make sure no row is missed.

The diagram below draws each gate with its standard symbol. Choose a gate from the buttons, then click the inputs A and B to change them. Wires carrying a 1 turn gold, and the matching row of the truth table is highlighted. Try NOT, AND and OR now and check that they match the tables above.

Task 2 - Complete the truth tables for NAND, NOR and XOR

Three more gates are used so often that they have their own symbols.

  • NAND means NOT AND. It is an AND gate followed by a NOT, so its output is the opposite of AND. The small circle on the symbol shows the inversion.
  • NOR means NOT OR. Its output is the opposite of OR.
  • XOR means exclusive OR. It outputs 1 when exactly one input is 1, in other words when the inputs are different.

Use the descriptions above to fill in the output column for each gate. Each answer is the value of Q for that row. When you have finished, check your answers with the gate explorer in Task 1.

NAND

ABQ
00
01
10
11

NOR

ABQ
00
01
10
11

XOR

ABQ
00
01
10
11

Which two gates give different outputs for exactly one row of the truth table?

OR and XOR agree on every row except A=1, B=1, where OR gives 1 and XOR gives 0. AND and NAND disagree on every row, because NAND is the inverse of AND.

An alarm should sound when a door sensor (A) is 1 or a window sensor (B) is 1, including when both are 1. Which gate is needed?

OR outputs 1 when either input or both inputs are 1. XOR would wrongly stay silent if the door and window were both opened.

A gate outputs 1 only when both of its inputs are 0. Which gate is it?

OR outputs 0 only for the 0, 0 row. NOR inverts that, so it outputs 1 only for the 0, 0 row.

Task 3 - Boolean expressions

Gates can be written as short algebraic expressions. This is Boolean algebra, named after the mathematician George Boole. The notation you will see most often is shown below.

NOTQ=A‾ANDQ=A⋅BORQ=A+BNANDQ=A⋅B‾NORQ=A+B‾XORQ=A⊕B\begin{array}{lll} \text{NOT} & Q = \overline{A} & \\ \text{AND} & Q = A \cdot B & \\ \text{OR} & Q = A + B & \\ \text{NAND} & Q = \overline{A \cdot B} & \\ \text{NOR} & Q = \overline{A + B} & \\ \text{XOR} & Q = A \oplus B & \end{array}

Be careful with the plus sign. In Boolean algebra 1 + 1 = 1, because it means OR, not addition. A bar over a whole expression inverts the result of that expression, so in NAND the AND happens first and the NOT is applied to its output.

Work out the value of Q for each expression.

A = 1, B = 0. Q = A · B =

A = 1, B = 0. Q = A + B =

A = 1, B = 1. Q = A ⊕ B =

A = 0, B = 1. Q = NOT(A · B) =

A = 1, B = 1, C = 0. Q = (A · B) + C =

A = 1, B = 0, C = 1. Q = A · (B + C) =

Write out the full truth table for Q = (NOT A) · B, where A is inverted first and the result is ANDed with B. Describe in one sentence when the output is 1.

Rows (A, B, Q): 0 0 0, 0 1 1, 1 0 0, 1 1 0. The output is 1 only when A is 0 and B is 1. Inverting A first means the AND sees a 1 from the A side only when A itself is 0.