{
  "id": "exclusive-or",
  "title": "Exclusive OR",
  "lang": "en",
  "source_lang": "en",
  "summary": "Exclusive OR is a logical operation that is true when exactly one of two inputs is true, equivalent to addition modulo two.",
  "category": "mathematics",
  "subcategory": "discrete-logic",
  "type": "concept",
  "aliases": [
    "XOR",
    "Exclusive disjunction",
    "EOR"
  ],
  "generated_at": "2026-10-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/exclusive-or/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/exclusive-or/",
      "json": "https://aiwiki.page/zh/exclusive-or.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Exclusive OR, commonly abbreviated XOR, is a binary operation in logic that returns true when its two inputs have different truth values and false when they have the same truth value. With false represented by 0 and true by 1, XOR produces 1 precisely when one input—but not both—is 1. It differs from inclusive OR, which also returns true when both inputs are true. XOR connects logical reasoning with binary computation and algebra. (xlinux.nist.gov)"
    },
    {
      "heading": "Logical definition",
      "text": "In propositional logic, XOR is usually written \\(p\\oplus q\\). Its truth table is:\n\n\\(p\\) | \\(q\\) | \\(p\\oplus q\\)\n0 | 0 | 0\n0 | 1 | 1\n1 | 0 | 1\n1 | 1 | 0\n\nThus, XOR is a “not equal” operation on Boolean values. (xlinux.nist.gov)\n\nUsing the standard operations of Boolean algebra, its definition can be expressed as\n\n\\[p\\oplus q=(p\\lor q)\\land\\neg(p\\land q).\\]\n\nEquivalently, it is \\((p\\land\\neg q)\\lor(\\neg p\\land q)\\): either the first proposition alone is true or the second alone is true. These formulas follow directly by checking the four truth-table rows. XOR’s negation, commonly called XNOR, is true when the inputs agree. (xlinux.nist.gov)\n\nThe distinction between exclusive and inclusive OR concerns whether simultaneous truth is allowed. For example, an exclusive choice between two options permits either option individually but rules out choosing both. The operation itself specifies truth conditions; it does not impose any temporal order or causal relationship between its inputs. (xlinux.nist.gov)"
    },
    {
      "heading": "Algebraic structure",
      "text": "XOR has four fundamental properties:\n\n\\[a\\oplus b=b\\oplus a, \\qquad (a\\oplus b)\\oplus c=a\\oplus(b\\oplus c),\\]\n\n\\[a\\oplus0=a, \\qquad a\\oplus a=0.\\]\n\nIt is therefore commutative and associative, has 0 as its identity, and makes every element its own inverse. In the terminology of group theory, the two Boolean values form an abelian group under XOR. These properties also hold for equal-length bit strings under componentwise XOR. (web.stanford.edu)\n\nCancellation consequently gives\n\n\\[(a\\oplus b)\\oplus b=a.\\]\n\nThis explains why applying the same XOR transformation twice restores the original value. XOR with 1 complements a single bit, whereas XOR with 0 leaves it unchanged. (web.stanford.edu)\n\nFor bits \\(a,b\\), XOR is addition in modular arithmetic:\n\n\\[a\\oplus b=(a+b)\\bmod2.\\]\n\nTogether with AND as multiplication, it supplies the arithmetic operations of the two-element finite field, \\(\\mathbb F_2\\). Bit strings of length \\(n\\) can accordingly be treated as elements of the vector space \\(\\mathbb F_2^n\\), with XOR as vector addition. This algebraic interpretation underlies binary coding and network-coding computations. (doi.org)\n\nAssociativity permits repeated XOR without specifying parentheses. The result is 1 exactly when an odd number of inputs are 1: each pair of 1s cancels. Importantly, this does not mean “exactly one input is true” for three or more inputs. For example, \\(1\\oplus1\\oplus1=1\\). This odd-parity behavior is a consequence of the binary operation’s identities. (web.stanford.edu)"
    },
    {
      "heading": "Connection with sets",
      "text": "In set theory, XOR corresponds to symmetric difference:\n\n\\[A\\triangle B=(A\\setminus B)\\cup(B\\setminus A).\\]\n\nAn element belongs to \\(A\\triangle B\\) exactly when it belongs to one of the sets but not both. The operation excludes their overlap, unlike ordinary union. Membership in the symmetric difference is therefore the XOR of the two membership values. (doi.org)\n\nSymmetric difference inherits XOR’s associativity and commutativity. Its identity is the empty set, and \\(A\\triangle A=\\varnothing\\). These identities allow calculations with sets to mirror calculations with Boolean values. (doi.org)"
    },
    {
      "heading": "Bitwise computation and digital circuits",
      "text": "Bitwise XOR applies the operation independently to corresponding bits of two binary numbers. It does not propagate carries between positions. For example, direct componentwise calculation gives\n\n\\[1010_2\\oplus1100_2=0110_2.\\]\n\nThe output marks positions where the inputs differ. In the programming language Python, the operator ^ performs bitwise XOR on integers; it is not an exponentiation symbol. (web.stanford.edu)\n\nAn XOR logic gate implements the same truth table electronically. In a half adder, XOR computes the sum bit \\(s=a\\oplus b\\), while AND computes the carry \\(c=a\\land b\\). Thus \\(1+1\\) produces sum 0 and carry 1, representing the binary result \\(10_2\\). A full adder includes an incoming carry, with sum \\(a\\oplus b\\oplus c_{\\mathrm{in}}\\). (www-inst.eecs.berkeley.edu)\n\nRepeated XOR also provides parity information used in error-correcting codes. From its odd-parity rule, a parity check detects any odd number of bit flips, but an even number can leave the parity unchanged. Parity alone therefore does not identify every possible error. (web.stanford.edu)"
    },
    {
      "heading": "Cryptographic use",
      "text": "In cryptography, XOR can combine a plaintext bit string \\(M\\) with a key string \\(K\\):\n\n\\[C=M\\oplus K,\\qquad M=C\\oplus K.\\]\n\nRecovery follows from cancellation. A binary one-time pad uses this construction with a secret, uniformly random key independent of the message, as long as the message, and never reused. Under these conditions it achieves perfect secrecy; XOR alone does not supply those conditions. (nvlpubs.nist.gov)\n\nIf the same key encrypts two messages, cancellation gives \\(C_1\\oplus C_2=M_1\\oplus M_2\\). This derived identity shows that key reuse exposes a relationship between the plaintexts rather than preserving the one-time pad’s secrecy guarantee. (nvlpubs.nist.gov)"
    },
    {
      "heading": "The XOR problem in machine learning",
      "text": "XOR is a standard example in machine learning of a classification problem lacking linear separability. The positive points \\((0,1)\\) and \\((1,0)\\) occupy opposite corners of a square, while \\((0,0)\\) and \\((1,1)\\) are negative. No straight line separates the two classes, so a single linear-threshold perceptron cannot represent XOR. (cs.cmu.edu)\n\nA multilayer perceptron can represent it using a hidden layer with nonlinear activation functions. One construction has hidden units detect OR and AND, then combines their outputs to exclude the case where both inputs are active. This illustrates how intermediate representations allow a neural network to express a decision rule unavailable to a single linear-threshold unit. (cs.cmu.edu)"
    }
  ],
  "links": [
    "logic",
    "propositional-logic",
    "truth-table",
    "boolean-algebra",
    "group-theory",
    "modular-arithmetic",
    "finite-field",
    "vector-space",
    "set-theory",
    "symmetric-difference",
    "empty-set",
    "bit",
    "binary-number",
    "programming-language",
    "logic-gate",
    "half-adder",
    "error-correcting-code",
    "cryptography",
    "one-time-pad",
    "perfect-secrecy",
    "machine-learning",
    "linear-separability",
    "perceptron",
    "multilayer-perceptron",
    "activation-function"
  ],
  "sources": [
    {
      "title": "xlinux.nist.gov/dads/HTML/xor.html",
      "url": "https://xlinux.nist.gov/dads/HTML/xor.html"
    },
    {
      "title": "web.stanford.edu/class/archive/cs/cs166/cs166.1266/math_identities",
      "url": "https://web.stanford.edu/class/archive/cs/cs166/cs166.1266/math_identities"
    },
    {
      "title": "doi.org/10.1016/j.ic.2020.104672",
      "url": "https://doi.org/10.1016/j.ic.2020.104672"
    },
    {
      "title": "www-inst.eecs.berkeley.edu/~ee16b/sp19/lab/sp19_lab_prelab1_solutions.pdf",
      "url": "https://www-inst.eecs.berkeley.edu/~ee16b/sp19/lab/sp19_lab_prelab1_solutions.pdf"
    },
    {
      "title": "web.stanford.edu/class/archive/cs/cs103/cs103.1156/lectures/01/Slides01.pdf",
      "url": "https://web.stanford.edu/class/archive/cs/cs103/cs103.1156/lectures/01/Slides01.pdf"
    },
    {
      "title": "nvlpubs.nist.gov/nistpubs/Legacy/IR/nistir4545.pdf",
      "url": "https://nvlpubs.nist.gov/nistpubs/Legacy/IR/nistir4545.pdf"
    },
    {
      "title": "cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/pubs/byte-hiddenlayer-1989.pdf",
      "url": "https://www.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/pubs/byte-hiddenlayer-1989.pdf"
    }
  ],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}