Glossary

XOR

TL;DR

XOR (Exclusive OR) is a logical operation that outputs true only when inputs differ, commonly used in programming, cryptography, and bitwise operations.


Concept

XOR (Exclusive OR) is a logical operation that outputs true only when the inputs differ (one is true and the other is false). It is a fundamental operation in computer science, used extensively in programming, cryptography, error detection, and digital circuit design.

XOR truth table:

A | B | A XOR B
--|---|--------
0 | 0 |   0
0 | 1 |   1
1 | 0 |   1
1 | 1 |   0

Key properties of XOR include:

  1. Commutative: A XOR B = B XOR A
  2. Associative: (A XOR B) XOR C = A XOR (B XOR C)
  3. Self-Inverse: A XOR A = 0
  4. Identity Element: A XOR 0 = A

Common applications of XOR:

  1. Bitwise Operations: XOR is used for bit manipulation in low-level programming

    • Toggling bits: x XOR 1 flips a bit
    • Checking differences: comparing two values bit by bit
  2. Cryptography: XOR is fundamental in many encryption algorithms

    • One-time pad encryption uses XOR for perfect secrecy
    • Stream ciphers often use XOR to combine plaintext with keystream
  3. Error Detection: XOR is used in parity checks and error-correcting codes

    • RAID systems use XOR for data redundancy
    • Checksum algorithms often incorporate XOR operations
  4. Swapping Values: XOR can swap two variables without temporary storage

    a = a XOR b
    b = a XOR b
    a = a XOR b
    
  5. Data Obfuscation: XOR is used to mask data or make it unreadable

    • Simple encryption techniques use XOR with a key
    • Data anonymization processes may use XOR operations
  6. Hash Functions: Some hash algorithms use XOR operations

    • Combining hash values to reduce collisions
    • Creating checksums for data integrity verification

In programming languages, XOR is typically represented by the caret symbol (^) for bitwise operations. Understanding XOR is essential for systems programming, cryptography, and algorithm development.