The Computing Series

The Concept

Logical operators combine statements into new statements. The four fundamental operators are:

Operator Symbol Name Evaluates to True when…
AND Conjunction Both operands are True
OR Disjunction At least one operand is True
NOT ¬ Negation The operand is False
XOR Exclusive Or Exactly one operand is True

These are complete definitions. There is no ambiguity. Each operator is fully defined by its truth table — a table listing every combination of input values and the resulting output. Truth tables are Chapter 3’s subject. For now, the definitions above are sufficient.

AND

P AND Q is True if and only if both P is True and Q is True. One False operand makes the whole expression False.

In programming: if user.is_authenticated and user.has_permission("write"): — access is granted only when both conditions hold.

OR

P OR Q is True if at least one of P, Q is True. Both can be True simultaneously. This is inclusive OR — it includes the case where both are True.

In programming: if request.is_cached or fallback_available: — service continues if either condition holds.

NOT

NOT is unary — it takes one operand and inverts its truth value. True becomes False; False becomes True.

In programming: if not user.is_banned: — the inverse of the stored condition.

XOR

XOR is True when exactly one operand is True. It is False when both are True and False when both are False. XOR is less common in high-level boolean logic, but it appears in:

  • Cryptography: bitwise XOR for key mixing and encryption
  • Error detection: parity bits in RAID-5 are computed with XOR
  • Toggle logic: state = state XOR True flips a boolean without an if-statement
# XOR implemented in Python (Python has no boolean XOR keyword, use !=)
def xor(p: bool, q: bool) -> bool:
    # XOR: exactly one of p, q must be True
    return p != q

# Alternatively, explicit:
def xor_explicit(p: bool, q: bool) -> bool:
    return (p or q) and not (p and q)

Read in the book →