Computer Science
Intermediate

XOR Operation

Exclusive OR: true when exactly one input is true.

Formula

a⊕b=(a∨b)∧¬(a∧b)a \oplus b = (a \lor b) \land \lnot(a \land b)

Variables

a, bBoolean bits

Example

1 ⊕ 1 = 0

How it works

The XOR operation compares two Boolean values and outputs 1 only when they differ. The expression (a ∨ b) ∧ ¬(a ∧ b) captures that idea by first checking that at least one input is true, then removing the case where both are true. In other words, it keeps the "one or the other" cases and excludes the "both" case.

When to use it

Use XOR when you need to detect a difference between two bits, such as in parity checks, simple adders, error detection, and bit toggling. It also appears in programming and digital logic whenever a condition should be true for exactly one of two possibilities.

Worked example, step by step

Let a = 1 and b = 0 Step 1: a ∨ b = 1 ∨ 0 = 1 Step 2: a ∧ b = 1 ∧ 0 = 0 Step 3: ¬(a ∧ b) = ¬0 = 1 Step 4: (a ∨ b) ∧ ¬(a ∧ b) = 1 ∧ 1 = 1 Final answer: 1 ⊕ 0 = 1

Common mistakes to avoid

  • Thinking XOR means "at least one true"; that is OR, not XOR
  • Forgetting that XOR is false when both inputs are true
  • Mixing up XOR with AND when working with bitwise operations in code

Did You Know?

XOR is reversible — apply it twice with the same key and you get the original back, the basis of simple ciphers.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026