Sub topic 13
OR, AND, and NOT gates with truth tables
This chapter assumes you know binary signals and logic levels (0 and 1) from the previous chapter, and that you can read a table of inputs and outputs. It covers the three basic gates and how to find the output of a combination of gates.
What a logic gate does
A logic gate is a circuit that takes one or more binary inputs and gives one binary output. The output depends only on the present inputs. A truth table lists the output for every possible input combination. A gate with n inputs has 2^n rows.
The OR gate
The OR gate gives output 1 if at least one input is 1. Its Boolean expression is Y = A + B. The "+" here means OR, not arithmetic addition, so 1 + 1 = 1.
| A | B | Y = A + B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
The AND gate
The AND gate gives output 1 only when all inputs are 1. Its expression is Y = A · B.
| A | B | Y = A · B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
The NOT gate
The NOT gate has one input and inverts it. Its expression is Y = A̅ (read "A bar"). It is also called an inverter.
| A | Y = A̅ |
|---|---|
| 0 | 1 |
| 1 | 0 |
Generating truth tables with code
The same tables can be produced by evaluating each gate on all input combinations.
from itertools import product
gates = {
"AND": lambda a, b: a & b,
"OR": lambda a, b: a | b,
}
for name, gate in gates.items():
print(name)
for a, b in product((0, 1), repeat=2):
print(a, b, "->", gate(a, b))
print("NOT")
for a in (0, 1):
print(a, "->", 1 - a)Combining gates
To find the output of a combination, work from the inputs toward the output. Write the intermediate value after each gate, then apply the next gate.
Example: Y = (A + B) · C̅. Take A = 1, B = 0, C = 1.
- A + B = 1 + 0 = 1.
- C̅ = 0.
- Y = 1 · 0 = 0.
For a full truth table, list all 8 rows of A, B, C and repeat these steps.
Common errors
- Treating OR as ordinary addition. In Boolean algebra, 1 + 1 = 1.
- Forgetting that a NOT gate acts on one input only. In (A + B)‾ the bar covers the whole OR result, which is a different circuit from A̅ + B̅.
- Reading a truth table with the wrong row order. Write inputs in binary counting order (00, 01, 10, 11) so no row is missed.
Practice
Exercise
For inputs A = 1, B = 1, C = 0, find Y = (A · B) + C̅.
Show answer
A · B = 1 · 1 = 1. C̅ = 1. Y = 1 + 1 = 1.
Exercise
A three-input AND gate has how many rows in its truth table, and for how many rows is the output 1?
Show answer
It has 2^3 = 8 rows. The output is 1 only in the row where all three inputs are 1, so one row.