Exclusive OR (XOR)
You'll learn how the XOR logical operator returns true only when exactly one of its inputs is true, making it different from regular OR.
What Makes XOR Special?
You've already learned about OR, which returns true when at least one operand is true. Exclusive OR (XOR) is more selective: it returns true only when exactly one operand is true, but not both.
Think of it like choosing between two desserts at a restaurant where the rule is "pick one or the other, but not both." If you pick cake OR ice cream (but not both), you've made a valid XOR choice. If you try to take both or neither, XOR becomes false.
The XOR Truth Table
Here's how XOR behaves with all possible inputs:
| A | B | A XOR B |
|---|---|---|
| F | F | F |
| F | T | T |
| T | F | T |
| T | T | F |
Notice that XOR is only true when the inputs differ from each other.
Why XOR Matters in Computing
XOR has powerful practical applications:
- Bit flipping: XORing a bit with 1 toggles it (0→1, 1→0)
- Comparison: Quickly check if two values are different
- Encryption: Simple XOR ciphers use this operator to scramble data
- Error detection: XOR helps identify corrupted bits in data transmission
- Swapping values: You can swap two variables without a temporary variable using XOR operations
The key insight: XOR detects difference. When bits or values match, XOR returns false. When they differ, XOR returns true.
Key Takeaway: XOR returns true when exactly one operand is true (inputs differ), making it essential for detecting changes, toggling bits, and basic encryption operations.