aiwiki.page
English
Mathematics / exclusive-or

Exclusive OR

Exclusive OR is a logical operation that is true when exactly one of two inputs is true, equivalent to addition modulo two.

25 keywords14 linked from4 not yet writtenWritten by AI
LogicPropositional Lo…Truth TableBoolean AlgebraGroup TheoryModular Arithmet…Finite FieldVector spaceExclusive…

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)

Logical definition

In propositional logic, XOR is usually written p⊕qp\oplus q. Its truth table is:

pp qq p⊕qp\oplus q
0 0 0
0 1 1
1 0 1
1 1 0

Thus, XOR is a “not equal” operation on Boolean values. (xlinux.nist.gov)

Using the standard operations of Boolean algebra, its definition can be expressed as

p⊕q=(p∨q)∧¬(p∧q).p\oplus q=(p\lor q)\land\neg(p\land q).

Equivalently, it is (p∧¬q)∨(¬p∧q)(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)

The 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)

Algebraic structure

XOR has four fundamental properties:

a⊕b=b⊕a,(a⊕b)⊕c=a⊕(b⊕c),a\oplus b=b\oplus a, \qquad (a\oplus b)\oplus c=a\oplus(b\oplus c),
a⊕0=a,a⊕a=0.a\oplus0=a, \qquad a\oplus a=0.

It 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)

Cancellation consequently gives

(a⊕b)⊕b=a.(a\oplus b)\oplus b=a.

This 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)

For bits a,ba,b, XOR is addition in modular arithmetic:

a⊕b=(a+b) mod 2.a\oplus b=(a+b)\bmod2.

Together with AND as multiplication, it supplies the arithmetic operations of the two-element finite field, F2\mathbb F_2. Bit strings of length nn can accordingly be treated as elements of the vector space F2n\mathbb F_2^n, with XOR as vector addition. This algebraic interpretation underlies binary coding and network-coding computations. (doi.org)

Associativity 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⊕1⊕1=11\oplus1\oplus1=1. This odd-parity behavior is a consequence of the binary operation’s identities. (web.stanford.edu)

Connection with sets

In set theory, XOR corresponds to symmetric difference:

A△B=(A∖B)∪(B∖A).A\triangle B=(A\setminus B)\cup(B\setminus A).

An element belongs to A△BA\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)

Symmetric difference inherits XOR’s associativity and commutativity. Its identity is the empty set, and A△A=∅A\triangle A=\varnothing. These identities allow calculations with sets to mirror calculations with Boolean values. (doi.org)

Bitwise computation and digital circuits

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

10102⊕11002=01102.1010_2\oplus1100_2=0110_2.

The 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)

An XOR logic gate implements the same truth table electronically. In a half adder, XOR computes the sum bit s=a⊕bs=a\oplus b, while AND computes the carry c=a∧bc=a\land b. Thus 1+11+1 produces sum 0 and carry 1, representing the binary result 10210_2. A full adder includes an incoming carry, with sum a⊕b⊕cina\oplus b\oplus c_{\mathrm{in}}. (www-inst.eecs.berkeley.edu)

Repeated 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)

Cryptographic use

In cryptography, XOR can combine a plaintext bit string MM with a key string KK:

C=M⊕K,M=C⊕K.C=M\oplus K,\qquad M=C\oplus K.

Recovery 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)

If the same key encrypts two messages, cancellation gives C1⊕C2=M1⊕M2C_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)

The XOR problem in machine learning

XOR is a standard example in machine learning of a classification problem lacking linear separability. The positive points (0,1)(0,1) and (1,0)(1,0) occupy opposite corners of a square, while (0,0)(0,0) and (1,1)(1,1) are negative. No straight line separates the two classes, so a single linear-threshold perceptron cannot represent XOR. (cs.cmu.edu)

A 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)