bitGateLab Digital · Learn
English
Open the simulator

Learn › Topic 1

Logic gates: NOT, AND, OR, NAND, NOR, XOR and XNOR

7 min read · bitGateLab Team

All of digital electronics, from a traffic light to a processor, is built from a handful of blocks that make decisions with zeros and ones: logic gates. Here you will meet the seven basic gates, how they are drawn, how they are written in Boolean algebra and what their truth tables look like. At the end you will see why a single type of gate, NAND or NOR, is enough to build all the others.

Logic levels: what 0 and 1 mean

In a digital circuit every signal can only be in one of two states. We call the low voltage (close to 0 V) logic 0 or false, and the high voltage (close to the supply, for example 5 V or 3.3 V) logic 1 or true. A gate takes one or more inputs and produces an output that depends only on the present value of its inputs: it remembers nothing. That is why gates are said to form combinational logic.

The most complete way to describe a gate is its truth table: a list of every possible combination of inputs together with the output for each one. With n inputs there are 2n combinations, so a two-input gate has 4 rows and a three-input gate has 8.

ABY ABY AY ANDORNOT
Distinctive-shape symbols for AND (flat back and rounded front), OR (curved back and pointed front) and NOT (triangle with a bubble).

The three fundamental gates

NOT (inverter)

It has a single input and outputs the opposite: a 0 in gives a 1 out, and a 1 in gives a 0 out. It is written Y = A (you will also see A' or ¬A). Its symbol is a triangle with a bubble at the tip; that bubble is the universal mark for "inversion happens here".

AY = A
01
10

AND (logical product)

The output is 1 only when all the inputs are 1. It is written as a product: Y = A·B or simply Y = AB. Think of two switches in series: the lamp lights only if both are closed.

OR (logical sum)

The output is 1 when at least one input is 1. It is written as a sum: Y = A + B. It is equivalent to two switches in parallel. Careful: in Boolean algebra 1 + 1 = 1, not 2.

Derived gates: NAND, NOR, XOR and XNOR

NAND is an AND followed by an inverter: Y = AB. Its output is 0 only when all the inputs are 1. NOR is an inverted OR: Y = A + B, and it is 1 only when all the inputs are 0. In the symbol, both carry the bubble at the output.

XOR (exclusive OR) is 1 when the inputs are different: Y = A ⊕ B = AB + AB. With more than two inputs, XOR is 1 when there is an odd number of ones, which is why it is used to compute parity bits. XNOR is its complement, Y = A ⊕ B = AB + A B, and it is 1 when the inputs are equal: it is a one-bit equality comparator.

Here is the combined truth table of the six two-input gates:

ABANDORNANDNORXORXNOR
00001101
01011010
10011010
11110001
A quick way to check: each inverted column (NAND, NOR, XNOR) is exactly the column of its base gate with the zeros and ones swapped, row by row.

Example: evaluating a small circuit. Compute Y = (A + B) · C for A = 0, B = 1, C = 1.

  1. OR: A + B = 0 + 1 = 1.
  2. Inversion: 1 = 0.
  3. AND with C: 0 · 1 = 0.

Result: Y = 0. With A = 0, B = 0, C = 1 the OR gives 0, the inverter gives 1 and the AND gives 1, so Y = 1.

NAND and NOR: universal gates

A gate is universal if copies of it alone are enough to build NOT, AND and OR, and therefore any logic function. NAND and NOR both have that property. This has a practical consequence: a manufacturer can produce a single type of cell and build the whole design with it, and you can solve a complete circuit with a single chip from the 74HC family (for example, the 74HC00 contains four two-input NAND gates).

Everything with NAND

Everything with NOR

Example: XOR from four NAND gates. Let N1 = AB, N2 = A·N1, N3 = B·N1 and Y = N2·N3. Check the four cases:

ABN1N2N3Y
001110
011101
101011
110110

The Y column matches the XOR column. Four gates, a single chip.

Common mistakes

Try it in the simulator

The best way to memorize these tables is to generate them yourself. In the circuit editor you can place a gate, toggle its inputs and see the output instantly; then compare with the truth table the tool computes.

  1. Place two inputs, a NAND gate and an output, and wire them together.
  2. Step the inputs through the four combinations and write down the output for each one.
  3. Generate the truth table of the circuit and compare it with the one in this article.
  4. Build the XOR from four NAND gates shown in the example and verify that its table matches that of a single XOR.
  5. If you want to take it to the virtual breadboard, place a NAND chip from the 74HC family, power it and watch the output LED.

Exercises

  1. Write the truth table of Y = A + B.
    Answer

    For AB = 00, 01, 10, 11 the output is 1, 0, 1, 1. It is 0 only when A = 0 and B = 1.

  2. How many rows does the truth table of a 4-input AND have, and in how many of them is the output 1?
    Answer

    24 = 16 rows; the output is 1 in only one of them (1111).

  3. Find the output of a three-input XOR with A = 1, B = 1, C = 1.
    Answer

    1 ⊕ 1 = 0 and 0 ⊕ 1 = 1. The output is 1, because there are three ones (an odd number).

  4. How many NOR gates do you need, at minimum with this method, to build a two-input AND?
    Answer

    Three: two NOR gates used as inverters (one for A and one for B) and a third NOR that takes A and B, whose output is A + B = AB.

To transform and simplify expressions built with these gates, continue with Boolean algebra.