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.
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".
| A | Y = A |
|---|---|
| 0 | 1 |
| 1 | 0 |
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:
| 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 |
Example: evaluating a small circuit. Compute Y = (A + B) · C for A = 0, B = 1, C = 1.
- OR: A + B = 0 + 1 = 1.
- Inversion: 1 = 0.
- 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
- NOT: tie the two inputs of a NAND together. If A goes into both, A·A = A.
- AND: a NAND followed by a NAND used as an inverter: AB = AB.
- OR: invert each input with a NAND and combine them in a third NAND: A·B = A + B (by De Morgan).
Everything with NOR
- NOT: tie the inputs of a NOR together: A + A = A.
- OR: a NOR followed by a NOR used as an inverter.
- AND: invert each input with a NOR and combine them in another NOR: A + B = AB.
Example: XOR from four NAND gates. Let N1 = AB, N2 = A·N1, N3 = B·N1 and Y = N2·N3. Check the four cases:
| A | B | N1 | N2 | N3 | Y |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 | 0 |
| 0 | 1 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 | 1 | 0 |
The Y column matches the XOR column. Four gates, a single chip.
Common mistakes
- Thinking that 1 + 1 = 2 in an OR. In Boolean logic the sum saturates: 1 + 1 = 1.
- Confusing AB (NAND, one bar over everything) with A B (two separate bars, which is equivalent to NOR). The length of the bar changes the result.
- Believing that a three-input XOR is 1 "when exactly one input is 1". It is actually 1 for an odd number of ones, and that includes the case 1, 1, 1.
- Leaving inputs unconnected on a real CMOS chip. A floating input may read as 0 or as 1 at random; tie it to ground or to the supply.
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.
- Place two inputs, a NAND gate and an output, and wire them together.
- Step the inputs through the four combinations and write down the output for each one.
- Generate the truth table of the circuit and compare it with the one in this article.
- Build the XOR from four NAND gates shown in the example and verify that its table matches that of a single XOR.
- 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
- 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.
- 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).
- 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).
- 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.