Mathwords logoMathwords

XOR (Exclusive Or) — Definition, Formula & Examples

XOR (Exclusive Or) is a logical operator that outputs true when exactly one of its two inputs is true, but not both. If both inputs match (both true or both false), XOR outputs false.

Given two propositions pp and qq, the exclusive disjunction pqp \oplus q is defined as (pq)¬(pq)(p \lor q) \land \lnot(p \land q). Equivalently, pqp \oplus q is true if and only if pp and qq have different truth values.

Key Formula

pq(pq)¬(pq)p \oplus q \equiv (p \lor q) \land \lnot(p \land q)
Where:
  • pp = First proposition (true or false)
  • qq = Second proposition (true or false)
  • \oplus = XOR operator (exclusive or)
  • \lor = OR operator (inclusive disjunction)
  • \land = AND operator (conjunction)
  • ¬\lnot = NOT operator (negation)

How It Works

XOR compares two statements and asks: are these different? To evaluate pqp \oplus q, check the truth values of pp and qq. If one is true and the other is false, the result is true. If both are true or both are false, the result is false. You can remember this by thinking of the word "exclusive" — it excludes the case where both inputs are true, unlike the regular OR (disjunction) which includes it. XOR appears constantly in computer science for tasks like toggling bits, error detection, and encryption.

Worked Example

Problem: Let p = "It is raining" (True) and q = "It is sunny" (True). Evaluate p ⊕ q.
Step 1: Evaluate the inclusive OR: p ∨ q.
pq=TT=Tp \lor q = T \lor T = T
Step 2: Evaluate the AND: p ∧ q.
pq=TT=Tp \land q = T \land T = T
Step 3: Negate the AND result.
¬(pq)=¬T=F\lnot(p \land q) = \lnot T = F
Step 4: Combine using AND to get the final XOR result.
pq=TF=Fp \oplus q = T \land F = F
Answer: p ⊕ q is False. Since both p and q are true, XOR returns false — they are not exclusively different.

Another Example

Problem: Let p = True and q = False. Evaluate p ⊕ q.
Step 1: Evaluate the inclusive OR.
pq=TF=Tp \lor q = T \lor F = T
Step 2: Evaluate the AND and negate it.
¬(pq)=¬(TF)=¬F=T\lnot(p \land q) = \lnot(T \land F) = \lnot F = T
Step 3: Combine to get XOR.
pq=TT=Tp \oplus q = T \land T = T
Answer: p ⊕ q is True. Exactly one input is true, so XOR returns true.

Why It Matters

XOR is essential in discrete mathematics courses and AP Computer Science, where it shows up in logic proofs and circuit design. In cryptography, XOR is the backbone of many encryption algorithms because applying it twice with the same key restores the original data. Bit-level XOR operations are also used in error-detecting codes, hash functions, and competitive programming problems.

Common Mistakes

Mistake: Treating XOR the same as regular OR and saying both-true gives true.
Correction: XOR specifically excludes the both-true case. When p and q are both true, pqp \oplus q is false. Only regular OR (pqp \lor q) returns true in that scenario.
Mistake: Forgetting that XOR of two false inputs is also false.
Correction: XOR requires exactly one true input. Two false inputs means the values are the same (not different), so XOR returns false.

Related Terms

  • DisjunctionInclusive OR; differs from XOR when both true
  • ConjunctionAND operator used in XOR's definition
  • ConditionalAnother binary logical connective (if-then)
  • ContrapositiveLogical equivalence used in proofs with connectives
  • ConverseRelated conditional form in logical reasoning
  • CounterexampleUsed to disprove logical statements involving XOR