Boolean Logic
AND, OR, NOT and XOR; truth tables; De Morgan's laws; short-circuit evaluation and the bitwise operators built on them.
Boolean logic works with only two values, true and false (1 and 0). It underlies both the hardware (logic gates are physical Boolean operators) and every if statement you write.
The core operators
| A | B | NOT A | A AND B | A OR B | A XOR B | A NAND B |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 1 | 1 | 0 | 0 |
- AND is true only when both inputs are true.
- OR is true when at least one is.
- XOR (exclusive or) is true when the inputs differ.
- NAND (not-and) is universal: every other gate can be built from NAND gates alone. So can NOR.
Notation
| Operation | Logic | Engineering | Java / JS | Python |
|---|---|---|---|---|
| NOT | ¬A | A̅ | !a | not a |
| AND | A ∧ B | A · B | a && b | a and b |
| OR | A ∨ B | A + B | a || b | a or b |
| XOR | A ⊕ B | A ⊕ B | a ^ b | a != b (for bools) |
Laws worth knowing
| Law | Form |
|---|---|
| Identity | A AND 1 = A; A OR 0 = A |
| Annulment | A AND 0 = 0; A OR 1 = 1 |
| Idempotence | A AND A = A |
| Complement | A AND NOT A = 0; A OR NOT A = 1 |
| Double negation | NOT NOT A = A |
| Distributive | A AND (B OR C) = (A AND B) OR (A AND C) |
| De Morgan | NOT (A AND B) = NOT A OR NOT B |
| De Morgan | NOT (A OR B) = NOT A AND NOT B |
Short-circuit evaluation
&& and || stop as soon as the answer is known:
a && b: ifais false,bis never evaluated.a || b: ifais true,bis never evaluated.
if (user != null && user.isActive()) { ... } // safe: no NPE when user is null
String name = input != null ? input : "guest";if user is not None and user.is_active:
...
name = value or "guest" # 'or' returns the first truthy operandif (user && user.isActive) { ... }
const name = input ?? "guest"; // only null/undefined fall back
const port = config.port || 8080; // careful: 0 also falls back!Bitwise operators: Boolean logic on every bit
The same operations applied position by position to integers:
| Expression | Binary | Result | Common use |
|---|---|---|---|
a & b | 1000 | 8 | Test or clear bits (masking) |
a | b | 1110 | 14 | Set bits, combine flags |
a ^ b | 0110 | 6 | Toggle bits, find differences |
~a | …0011 | −13 | Invert all bits |
a << 1 | 11000 | 24 | Multiply by 2 |
a >> 2 | 0011 | 3 | Divide by 4 (floor) |
Flags pack many booleans into one integer: Unix file permissions (rwx = 4 + 2 + 1, so chmod 755), feature flags, and network header fields. More techniques: Bit manipulation.
From logic to hardware
Gates are made of transistors, and gate circuits compute anything. For example, adding two bits:
- sum = A XOR B
- carry = A AND B
That's a half adder. Chain full adders and you can add 64-bit numbers — the heart of the CPU's ALU. See Logic gates & circuits.
- AND, OR, NOT (and XOR) are the building blocks; NAND alone is universal.
- De Morgan: negating AND/OR flips the operator and negates each operand.
&&and||short-circuit, which makes null checks safe.- Bitwise operators apply Boolean logic to each bit; masks and flags rely on them.
- Gates are Boolean operators in hardware; adders are made from XOR and AND.