Gates from switches
Two transistor switches sit one after the other, just like in the transistors lesson. Current reaches the bulb only when switch A and switch B are both on. That rule is called AND.
Now each switch has its own path to the bulb. Current gets through if switch A or switch B is on, or both are. That rule is called OR.
Symbols and truth tables
We write on as 1 and off as 0; each is one bit. A logic gate is a tiny circuit that follows one rule. Each gate has a symbol: bits go in on the left, the answer comes out on the right.
NOT has one input and gives the opposite: 0 becomes 1 and 1 becomes 0. A truth table lists every possible input and the output it gives, one row each.
AND gives 1 only when A and B are both 1. Two inputs make four rows, from 0 0 up to 1 1, and only the last row lights the output.
OR gives 1 when A is 1, when B is 1, or when both are. Only the 0 0 row leaves the output dark.
NAND means NOT AND: it flips every answer of AND. So it gives 0 only when both inputs are 1. The small circle on the symbol means flip.
NOR means NOT OR. It gives 1 only when both inputs are 0. Its symbol is the OR shape with the same small circle.
XOR, short for exclusive OR, gives 1 when the two inputs are different and 0 when they match. Its symbol is OR with an extra curved line at the back.
Inside a gate
Real chips build gates with CMOS: two kinds of transistor working as a pair. A CMOS NAND uses four, two in series and two in parallel, the two patterns from the start. A CMOS NOT uses two.
The rule, row by row
;; step 1/10 · series-and ;; about: Switches in series battery → switch A → switch B → bulb A off, B off → bulb off A off, B on → bulb off A on, B off → bulb off A on, B on → bulb ON One open switch breaks the only path.
Inside a chip, 1 and 0 are two voltage levels (high and low); here the bulbs and lamps just show them. A lit wire means it carries a 1. In Python, &, | and ^ on the numbers 0 and 1 work as AND, OR and XOR. For NOT write 1 - a, because ~a in Python gives -(a + 1).