Logic gates and Boolean algebra
AND, OR, NOT, XOR and De Morgan's laws. Schematics and the conditions in C code follow the same rules.
Basic gates
| Gate | Output is 1 when | C operator |
|---|---|---|
| AND | Both are 1 | &, && |
| OR | At least one is 1 | \|, \|\| |
| NOT | The input is 0 | ~, ! |
| XOR | The two differ | ^ |
With NAND alone, or NOR alone, any logic circuit can be built, and the inside of real chips is made of these gates.
De Morgan's laws
- NOT(A AND B) = (NOT A) OR (NOT B)
- NOT(A OR B) = (NOT A) AND (NOT B)
In circuits they are used when handling active-low signals, and in code when inverting a complicated condition. !(ready && !error) is the same as !ready || error.
Where you meet them on schematics
An OR that merges several reset signals into one, a decoder that makes chip-select signals, an XOR that makes a pulse when two signals differ. Even with an MCU, the surrounding circuit often includes small logic-gate chips (the 74 series).