Logic Gates and Truth Tables

Every part of a computer, from the adder in the processor to the cells in memory, is built from tiny switches called logic gates. A gate takes one or two bits in and gives one bit out, following a fixed rule. In this lesson you will learn the seven gates named in the CAPE syllabus, how to write their rules as truth tables and Boolean expressions, and how to work out what a circuit of several gates does. Every gate is also written as a line of C, so you can test your tables on a computer.

What you will learn

  • What a truth table is, how many rows it needs and how to order the rows.
  • The symbol, rule, expression and truth table of NOT, AND, OR, NAND, NOR, XOR and XNOR (which the syllabus calls XAND).
  • How to complete the truth table of a circuit made from several gates, using working columns.
  • How to write the Boolean expression for a circuit, and how to build a circuit from a truth table.
  • Why NAND and NOR are called universal gates.
  • How C writes the same rules with &&, ||, ! and !=.

Part 1: Bits, switches and truth tables

Inside a chip, a bit is a voltage on a wire: a high voltage stands for 1 (true, on) and a low voltage stands for 0 (false, off). A logic gate is a small circuit of transistors that looks at its input wires and sets its output wire to 1 or 0.

A truth table lists every possible combination of inputs and the output for each one. Each input can only be 0 or 1, so:

  • 1 input needs 2 rows; 2 inputs need 4 rows; 3 inputs need 8 rows.
  • In general, n inputs need 2n rows.
  • Write the rows in order by counting in binary: 000, 001, 010, 011, 100, 101, 110, 111. The rightmost input column alternates 0, 1, 0, 1; the next column goes 0, 0, 1, 1; the leftmost goes four 0s then four 1s. This way you never miss or repeat a row.

Part 2: The seven gates

The table below shows each gate with its standard (American) symbol, its rule in words and its Boolean expression. A small circle (a “bubble”) on a symbol’s output means NOT: it flips the result. CAPE accepts the usual notations: AND as A·B or A ∧ B, OR as A + B or A ∨ B, and NOT as A′, Ā or ∼A. The syllabus’s logic-symbols page uses ∧, ∨ and ∼.

GateSymbolRuleExpression
NOTNOT gate symbolOutput is the opposite of the input. Also called an inverter.X = Ā (NOT A)
ANDAND gate symbolOutput is 1 only when all inputs are 1.X = A·B
OROR gate symbolOutput is 1 when at least one input is 1.X = A + B
NANDNAND gate symbolNOT AND. Output is 0 only when all inputs are 1.X = NOT (A·B)
NORNOR gate symbolNOT OR. Output is 1 only when all inputs are 0.X = NOT (A + B)
XORXOR gate symbolExclusive OR. Output is 1 when the two inputs are different.X = A ⊕ B
XNORXNOR gate symbolEquivalence. Output is 1 when the two inputs are the same.X = NOT (A ⊕ B)

Here are all the two-input truth tables side by side. Learn them as patterns of the output column, read from the top row (00) to the bottom row (11):

ABANDORNANDNORXORXNOR
00001101
01011010
10011010
11110001
  • AND is 0001: a single 1 at the bottom. OR is 0111: a single 0 at the top.
  • NAND (1110) and NOR (1000) are AND and OR with every output flipped.
  • XOR (0110) is 1 in the middle two rows, where the inputs differ. XNOR (1001) is its opposite.

A note on “XAND”. The syllabus list of gates includes XAND. That name is not standard in electronics. It is normally taken to mean the gate that gives 1 when both inputs are the same, which most books call XNOR or the equivalence gate. Learn it as XNOR, and recognise both names in a question. If a question ever defines XAND differently, follow the question’s definition.

Try it 1Gate explorer

Choose a gate, then tap A and B to switch them between 0 and 1. The output and the matching row of the truth table (highlighted) change straight away.

Output X

Part 3: Circuits of several gates

Real circuits join gates together: the output of one gate becomes the input of another. To find what a circuit does, add working columns to the truth table, one for each gate output inside the circuit, and fill them in from the inputs towards the final output.

Worked example 1: X = (A AND B) OR (NOT C)

There are three inputs, so the table needs 23 = 8 rows. First fill in A·B (1 only in the rows where A and B are both 1). Then fill in NOT C (the opposite of the C column). Finally OR the two working columns together.

ABCA·BNOT CX
000011
001000
010011
011000
100011
101000
110111
111101

Read the result back in words: X is 1 whenever C is 0, and also when A and B are both 1. Writing a sentence like this is a good way to check a table.

From a circuit diagram to an expression

Start at the inputs and label the output wire of every gate with what it calculates. Work forward until you reach the final output. For example, if A and B go into a NOR gate, and its output goes into an AND gate together with C, label the NOR output NOT (A + B), so the final output is X = NOT (A + B) · C. Use brackets so the order is clear: NOT is done first, then AND, then OR, unless brackets say otherwise (just as × comes before + in arithmetic).

From a truth table to a circuit

To design a circuit, look at the rows where the output is 1. Write one AND term for each such row, using NOT on any input that is 0 in that row, then OR all the terms together. This is called the sum of products form.

Worked example 2. XOR is 1 in two rows: A = 0, B = 1 and A = 1, B = 0. The terms are (NOT A)·B and A·(NOT B), so XOR = (NOT A · B) + (A · NOT B). That is an XOR gate built from two NOT gates, two AND gates and one OR gate. You can check it in Try it 2 (choose the third expression).

Try it 2Fill in the truth table

Choose an expression. Tap each ? in the X column to set it to 1 or 0, then tap Check. Stuck? Show the working columns.

Part 4: NAND and NOR, the universal gates

NAND and NOR are called universal gates, because any circuit at all can be built using only NAND gates (or only NOR gates). Chip makers like this: a whole chip can be made from one kind of gate. Three facts are enough to show it for NAND:

  • NOT from NAND: join both inputs together. A NAND A = NOT A.
  • AND from NAND: a NAND followed by a NAND-as-NOT. NOT (A NAND B) = A·B.
  • OR from NAND: invert each input first, then NAND them. (NOT A) NAND (NOT B) = A + B. (This is one of De Morgan’s laws.)

Check the first fact with the NAND truth table: in the rows where A and B are equal (00 and 11), the output is 1 and 0, the opposite of A.

Part 5: Logic gates in C

C has no separate type for true and false. It uses whole numbers: 0 means false and any other value means true. The logical operators always give 0 or 1, so they behave exactly like gates. (Review Variables, format specifiers and printf if printf and %d are new to you.)

GateC expressionNotes
ANDa && bLogical AND
ORa || bLogical OR
NOT!aLogical NOT
NAND!(a && b)NOT applied to AND
NOR!(a || b)NOT applied to OR
XORa != bTrue when the bits differ (for values 0 and 1)
XNORa == bTrue when the bits are the same

This program prints the two-input truth tables. Each pass of the inner loop is one row.

#include <stdio.h>

/* Truth tables for six gates.
   In C, 0 means false and 1 means true. */
int main(void)
{
    int a, b;

    printf("A B | AND OR NAND");
    printf(" NOR XOR XNOR\n");
    for (a = 0; a <= 1; a++) {
        for (b = 0; b <= 1; b++) {
            int and_ = a && b;
            int or_ = a || b;
            int xor_ = (a != b);
            printf("%d %d |", a, b);
            printf("  %d   %d", and_, or_);
            printf("   %d ", !and_);
            printf("   %d", !or_);
            printf("   %d", xor_);
            printf("   %d\n", !xor_);
        }
    }
    return 0;
}

Output:

A B | AND OR NAND NOR XOR XNOR
0 0 |  0   0   1    1   0   1
0 1 |  0   1   1    0   1   0
1 0 |  0   1   1    0   1   0
1 1 |  1   1   0    0   0   1

The output matches the table in Part 2. The next program works out Worked example 1. Instead of three nested loops it counts a row number r from 0 to 7 and takes A, B and C from the bits of r: A = r / 4, B = (r / 2) % 2 and C = r % 2. This is the same "count in binary" rule you use to write the rows by hand.

#include <stdio.h>

/* X = (A AND B) OR (NOT C)
   Row r counts 0 to 7 in binary, so
   A, B and C are the bits of r. */
int main(void)
{
    int r;

    printf("A B C | A.B NOT C | X\n");
    for (r = 0; r < 8; r++) {
        int a = r / 4;
        int b = (r / 2) % 2;
        int c = r % 2;
        int p = a && b;  /* middle */
        int q = !c;      /* middle */
        int x = p || q;
        printf("%d %d %d |", a, b, c);
        printf("  %d    %d  ", p, q);
        printf(" | %d\n", x);
    }
    return 0;
}

Output:

A B C | A.B NOT C | X
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

Try this: change the line int x = p || q; so the program prints the table for X = NOT (A + B) · C, then check it with Try it 2. C also has bitwise operators (&, |, ^ and ~) that apply a gate to every bit of a number at once. You will meet them when we look at registers.

Try it 3Gate drill

Name the gate from its output column, or work out an expression for given inputs. Answers are 0 or 1 for the expression questions.

Summary

IdeaKey points
Truth tableLists every input combination: n inputs need 2n rows. Order the rows by counting in binary.
AND, OR, NOTAND: 1 only if all inputs are 1. OR: 1 if any input is 1. NOT: flips the bit.
NAND, NORAND and OR followed by NOT. Universal gates: any circuit can be built from either one alone.
XOR, XNORXOR: 1 when the inputs differ. XNOR (the syllabus’s XAND, equivalence): 1 when they are the same.
CircuitsAdd a working column for each gate. Work from the inputs to the output.
DesignSum of products: one AND term per row with output 1, then OR the terms.
In C&&, ||, !, != and == act as gates on 0 and 1.

Practice questions

1. How many rows does the truth table of a circuit with 4 inputs need?

Show answer

24 = 16 rows.

2. Give the output column of a NOR gate for the rows 00, 01, 10, 11.

Show answer

1, 0, 0, 0. NOR is 1 only when both inputs are 0.

3. A and B go into an AND gate. A and B also go into a NOR gate. The two outputs go into an OR gate. Write the expression and the truth table. What single gate does it match?

Show answer

X = A·B + NOT (A + B). Rows 00, 01, 10, 11 give 1, 0, 0, 1. It matches XNOR: the output is 1 when the inputs are the same.

4. Complete the truth table for X = NOT A · (B + C). For which input rows is X = 1?

Show answer

X = 1 only when A = 0 and at least one of B, C is 1: rows 001, 010 and 011.

5. Show how to make a NOT gate using one NOR gate.

Show answer

Join both inputs of the NOR gate to A. Then X = NOT (A + A) = NOT A: when A = 0 the output is 1, and when A = 1 it is 0.

6. A circuit must give 1 only when exactly one of A and B is 1. Write a sum of products expression for it and name the gate.

Show answer

X = (NOT A · B) + (A · NOT B). It is XOR.

7. In C, what does printf("%d", !(1 || 0)); print, and which gate is this?

Show answer

0. 1 || 0 is 1, and ! flips it to 0. NOT applied to OR is a NOR gate.

Homework

  1. Draw the symbols for all seven gates from memory, then write the output column of each one under it. Check yourself against Part 2.
  2. Make up your own 3-input circuit with three gates. Draw it, write its expression and complete its truth table with working columns. Then change the circuit.c program to print the same table and compare.
  3. Using only NAND gates, draw circuits for NOT, AND and OR. Check each one with a truth table.
  4. For the expression X = (A + B) · NOT C, write a sentence in plain English that describes when X is 1 (for example, as a rule for when an alarm should ring).

Exam tips

Specimen Unit 1 Paper 01 (2021), Item 9. A multiple-choice item that shows four truth tables and asks which one belongs to a NAND gate. Know the output column 1, 1, 1, 0 by heart.

Specimen Unit 1 Paper 01 (2021), Item 3. Shows a small circuit and asks which Boolean expression it implements. Label each gate’s output wire, as in Part 3, before you look at the options.

Specimen Unit 1 Paper 02 (2021), Question 1(b). Asks you to construct the truth table for a circuit made of several basic gates. Show your working columns: they earn method marks even if the final column has a slip.

2009 Unit 1 Paper 02, Question 1. Early parts ask for the truth tables of basic gates and for an XOR gate built from AND, OR and NOT gates, which is exactly Worked example 2. See my worked solution: 2009 U1 Q1.

2024 Unit 1 Paper 02, Question 2. Begins with a circuit whose truth table turns out to match a single gate. Completing the table and then naming the gate is a common two-step pattern. See 2024 U1 Q2.

More worked answers are on the CS Solutions page. General advice: always order your rows by counting in binary, write the number of rows (2n) before you start, and use brackets in expressions.

Previous: Power Protection and Types of Computers. Next: Number Bases: Binary, Octal and Hexadecimal. See also: Logic Gates – Formal Introduction (an older introduction on this site).

Syllabus link: CAPE Computer Science (CXC A19/U2/21), Unit 1, Module 1: Computer Architecture. Specific Objective 2: describe the basic building blocks of a computer (truth tables; logic gates AND, OR, NOT, NAND, NOR, XOR, XAND). Specific Objective 3: interpret the interaction among the basic building blocks (logic gates).

Video search terms

Search YouTube for: "logic gates truth tables explained", "NAND universal gate", "sum of products from truth table", "logic circuit to Boolean expression".

Try it 4Quick quiz

Tap an answer. All eight questions are self-checks on this lesson.

Attributions to media used in this post:

  • Gate symbols (AND, OR, NOT, NAND, NOR, XOR, XNOR, ANSI style): jjbeard, public domain, via Wikimedia Commons (File:AND ANSI.svg, File:OR ANSI.svg, File:NOT ANSI.svg, File:NAND ANSI.svg, File:NOR ANSI.svg, File:XOR ANSI.svg, File:XNOR ANSI.svg).

Updates

2026/11/23

- Removed the Print this lesson button.

- Published.

2026/11/11

- Draft written (local file, not yet on the site).

© 2026 Vedesh Kungebeharry. All rights reserved.

Leave a Reply