Logic gates

Computer Science · Foundations

Question 1 of 10

0

    In the circuit with gates 1, 2 and 3, name gate 3.

    Diagram for the question: In the circuit with gates 1, 2 and 3, name gate 3.

    Capital letters and accents are ignored.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Logic gates explained

    Logic gates are the basic building blocks of computer hardware. Each gate takes one or two inputs, each of which is 0 or 1, and produces one output that follows a fixed rule. Combining gates lets a computer add numbers, make decisions and control memory.

    The main gates are:

    • AND: the output is 1 only when both inputs are 1.
    • OR: the output is 1 when at least one input is 1.
    • NOT: the output is the opposite of its single input.
    • XOR: the output is 1 when the two inputs are different.
    • NAND and NOR: these are NOT AND and NOT OR. NAND is especially useful, because any logic circuit can be built from NAND gates alone.

    A truth table lists every combination of inputs with the matching output. With nn inputs there are 2n2^n combinations, so two inputs give 4 rows and three inputs give 8.

    A common mistake is to confuse OR with XOR. For inputs 1 and 1, OR gives 1 but XOR gives 0.

    Worked example

    A half adder adds two single bits, using an XOR gate for the sum and an AND gate for the carry. Add 1 and 1.

    1. Sum = A XOR B = 1 XOR 1 = 0.
    2. Carry = A AND B = 1 AND 1 = 1.
    3. The carry is the left digit and the sum is the right digit, so the answer is 102=210_2 = 2 in decimal.

    Key facts

    The practice questions on this page use diagrams. You are asked to name a gate from its shape, to follow 0s and 1s through a small circuit, or to say which single gate a circuit is equivalent to. The shapes are:

    • AND: flat back, rounded front.
    • OR: curved back, pointed front.
    • NOT: a triangle with a small circle. It is the only gate with one input.
    • NAND and NOR: the AND or OR shape with a small circle on the output. The circle means "invert".
    • XOR: the OR shape with an extra curved line at the inputs.

    Equivalences that the questions rely on:

    • AND followed by NOT is NAND, and OR followed by NOT is NOR.
    • NOT followed by NOT cancels out, so NOR followed by NOT is the same as OR.
    • A NAND with both inputs joined to one wire is a NOT gate, because NAND(A, A) = NOT A.
    • Two NAND gates, with the second used as a NOT, make an AND gate.

    To trace a circuit, label the wire after each gate (W, X and so on), work out each wire from left to right, and write the final output last.

    Worked example 2

    A circuit has an OR gate on inputs A and B, a NOT gate on input C, and an AND gate that combines the two results. So Q=(A OR B) AND NOT CQ = (A \text{ OR } B) \text{ AND NOT } C. Write the truth table and count the rows where Q=1Q = 1.

    With 3 inputs there are 23=82^3 = 8 rows. Let W = A OR B and X = NOT C.

    A B C | W X | Q
    0 0 0 | 0 1 | 0
    0 0 1 | 0 0 | 0
    0 1 0 | 1 1 | 1
    0 1 1 | 1 0 | 0
    1 0 0 | 1 1 | 1
    1 0 1 | 1 0 | 0
    1 1 0 | 1 1 | 1
    1 1 1 | 1 0 | 0
    

    Q is 1 in 3 rows. A shortcut: Q needs C = 0 (4 rows), and also A and B not both 0, which removes one row, leaving 4−1=34 - 1 = 3.

    Worked example 3

    A different circuit has an AND gate on A and B, giving W, and an OR gate on W and C. So Q=(A AND B) OR CQ = (A \text{ AND } B) \text{ OR } C. For how many of the 8 input combinations is Q=1Q = 1? What is Q for A = 1, B = 0, C = 0?

    1. If C = 1 then Q = 1, whatever A and B are. That is 4 combinations.
    2. If C = 0 then Q = W = A AND B, which is 1 only for A = 1 and B = 1. That adds 1 combination.
    3. Total: 4+1=54 + 1 = 5 of the 8 combinations.
    4. For A = 1, B = 0, C = 0: W = 1 AND 0 = 0, then Q = 0 OR 0 = 0.

    Worked example 4

    Show how NAND gates alone can make NOT, AND and OR.

    1. NOT: join both inputs of a NAND to A. NAND(A, A) is 0 when A = 1 and 1 when A = 0, so it is NOT A.
    2. AND: feed NAND(A, B) into a second NAND with its inputs joined. This is NOT(NOT(A AND B)), which is A AND B.
    3. OR: make NOT A and NOT B with two NANDs, then feed both into a third NAND. For A = 0 and B = 0, the inputs to the third gate are 1 and 1, and NAND(1, 1) = 0. For A = 1 and B = 0, they are 0 and 1, and NAND(0, 1) = 1. This is A OR B.

    This is why NAND is called a universal gate.

    Common mistakes

    • Reading a small circle as part of a separate NOT gate. On the output of AND or OR it turns the gate into NAND or NOR.
    • Mixing up the flat-backed AND and the curved-backed OR.
    • Forgetting that XOR is 0 when both inputs are 1.
    • Tracing right to left. Always start at the inputs.
    • Counting the 8 rows wrongly. Three inputs always give 23=82^3 = 8 rows, not 6.

    Exam tips

    • Write the value on every internal wire, not only the final output.
    • For a "how many combinations" question, list all 8 rows in counting order (000, 001, 010, ...) so none is missed.
    • For an "equivalent single gate" question, check one row where AND and OR differ (A = 1, B = 0) and one where NAND and NOR differ.
    • Say "A XOR B" rather than "A or B" if the exam wants exclusive OR.

    What you will practise

    41 diagrams. Type the label for each part and get instant feedback.

    Example questions

    • In the gate symbols diagram, name the gate labelled A.
    • In the gate symbols diagram, name the gate labelled B.
    • In the gate symbols diagram, name the gate labelled C.
    • In the gate symbols diagram, name the gate labelled D.
    • In the gate symbols diagram, name the gate labelled E.
    • In the gate symbols diagram, name the gate labelled F.
    • In the half adder diagram, name the gate labelled X.
    • In the half adder diagram, name the gate labelled Y.

    …and 33 more.