Exclusive or returns one exactly when its two input bits differ. Its truth table gives , , , and . The identitiesexpress exclusive or using logical conjunction, logical disjunction, and logical negation. The first expression accepts at least one input and then excludes both; the second explicitly lists the two accepted input pairs.
For numerical bits, exclusive or is addition in modular arithmetic modulo two:The binary operation is commutative and associative, because addition modulo two has those properties. It satisfies , , and . These identities explain why a bit can be toggled and restored with the same operation.
On binary strings of equal length, bitwise exclusive or applies the one-bit rule separately at each position. For example,A bit mask of toggles exactly the middle two bits. Applying the mask twice restores the original binary string: each selected bit changes twice, and each unselected bit stays fixed. This positional operation is distinct from taking the exclusive or of the overall truth values of two integers.
For bits , repeated exclusive or computes the parity of the number of ones:An induction proves this: the first bit gives its own parity, and appending a zero preserves it while appending a one changes it. The binary string has three ones, so its parity bit is one. Append that parity bit to obtain , whose six bits have even parity. Flipping the second bit produces and changes the combined exclusive or to one, detecting an error. Flipping the third bit as well produces , with four ones and combined exclusive or zero: an even number of flips can escape this check.
For three inputs, repeated exclusive or means an odd number of true inputs, rather than exactly one: . The “exactly one” description applies to the two-input exclusive or. This distinction matters when translating a requirement that exactly one option be selected into a program condition.
Articles by others on the same topic
There are currently no matching articles.