Logic

Propositional Logic

Connectives
Symbol    Name              Read as             Programming
──────────────────────────────────────────────────────────────
¬p        Negation          "not p"             !p, ~p
p ∧ q     Conjunction       "p and q"           p && q, p & q
p ∨ q     Disjunction       "p or q"            p || q, p | q
p → q     Implication       "if p then q"       (!p || q)
p ↔ q     Biconditional     "p if and only if q" p == q
p ⊕ q     Exclusive or      "p xor q"           p ^ q
Truth tables
p   q   ¬p   p∧q   p∨q   p→q   p↔q   p⊕q
─────────────────────────────────────────────
T   T    F    T     T     T     T      F
T   F    F    F     T     F     F      T
F   T    T    F     T     T     F      T
F   F    T    F     F     T     T      F
Implication (p → q) — the tricky one
p → q is FALSE only when p is TRUE and q is FALSE

"If it rains, the ground is wet"
  Rain + wet    = TRUE   (expected)
  Rain + dry    = FALSE  (promise broken)
  No rain + wet = TRUE   (sprinkler — no claim about non-rain)
  No rain + dry = TRUE   (nothing to verify)

Equivalent forms:
  p → q  ≡  ¬p ∨ q  ≡  ¬q → ¬p (contrapositive)

NOT equivalent:
  p → q  ≢  q → p    (converse — different statement)
  p → q  ≢  ¬p → ¬q  (inverse — different statement)

Logical Equivalences

Laws of logic
Identity:        p ∧ T ≡ p          p ∨ F ≡ p
Domination:      p ∧ F ≡ F          p ∨ T ≡ T
Idempotent:      p ∧ p ≡ p          p ∨ p ≡ p
Double negation: ¬(¬p) ≡ p
Commutative:     p ∧ q ≡ q ∧ p      p ∨ q ≡ q ∨ p
Associative:     (p∧q)∧r ≡ p∧(q∧r)  (p∨q)∨r ≡ p∨(q∨r)
Distributive:    p∧(q∨r) ≡ (p∧q)∨(p∧r)
                 p∨(q∧r) ≡ (p∨q)∧(p∨r)
De Morgan's:     ¬(p ∧ q) ≡ ¬p ∨ ¬q
                 ¬(p ∨ q) ≡ ¬p ∧ ¬q
Absorption:      p ∧ (p ∨ q) ≡ p
                 p ∨ (p ∧ q) ≡ p
Complement:      p ∧ ¬p ≡ F
                 p ∨ ¬p ≡ T
De Morgan’s in programming
# De Morgan's law applies directly to code
# !(A && B) == (!A || !B)
# !(A || B) == (!A && !B)

# Instead of:
if ! ([ -f "$file" ] && [ -r "$file" ]); then echo "bad"; fi

# Equivalent (De Morgan's):
if [ ! -f "$file" ] || [ ! -r "$file" ]; then echo "bad"; fi

Predicate Logic

Quantifiers
∀x P(x)    "For all x, P(x) is true"       Universal quantifier
∃x P(x)    "There exists an x such that P(x)" Existential quantifier
∃!x P(x)   "There exists exactly one x"     Unique existential

Negation of quantifiers:
  ¬(∀x P(x)) ≡ ∃x ¬P(x)
  ¬(∃x P(x)) ≡ ∀x ¬P(x)

"Not all students passed" = "Some student failed"
"No one is perfect" = "For all people, that person is not perfect"
Nested quantifiers
∀x ∀y P(x,y)    "For every x and every y"
∀x ∃y P(x,y)    "For every x there exists a y"   (y can depend on x)
∃x ∀y P(x,y)    "There exists an x that works for all y"  (stronger)
∃x ∃y P(x,y)    "There exist some x and y"

Order matters!
  ∀x ∃y (y > x)     TRUE in integers (always a bigger number)
  ∃y ∀x (y > x)     FALSE in integers (no single biggest number)

Boolean Algebra and Circuits

Connection to digital logic
Proposition    Logic Gate    Programming
─────────────────────────────────────────
p ∧ q          AND gate      p & q
p ∨ q          OR gate       p | q
¬p             NOT gate      ~p
p ⊕ q          XOR gate      p ^ q
¬(p ∧ q)       NAND gate     ~(p & q)
¬(p ∨ q)       NOR gate      ~(p | q)
Bit manipulation as logic
# Boolean algebra directly maps to bitwise operations
# Mask (AND): keep specific bits
echo $(( 0xFF & 0x0F ))        # 0x0F — low nibble

# Set bits (OR): turn on specific bits
echo $(( 0x00 | 0x80 ))        # 0x80 — set bit 7

# Toggle bits (XOR): flip specific bits
echo $(( 0xFF ^ 0x0F ))        # 0xF0 — toggle low nibble

# Test if bit is set (AND then compare)
val=0x84
if (( val & 0x80 )); then echo "bit 7 set"; fi