TOPICS
Search

XOR


XORGate

XOR is a connective in logic known as the "exclusive or," or exclusive disjunction. It yields true if exactly one (but not both) of two conditions is true. The XOR operation does not have a standard symbol, but is sometimes denoted A xor B (this work) or A direct sum B (Simpson 1987, pp. 539 and 550-554). A xor B is read "A aut B," where "aut" is Latin for "or, but not both." The circuit diagram symbol for an XOR gate is illustrated above. In set theory, A xor B is typically called the symmetric difference. The XOR function is implemented as Xor[predicate1, predicate2, ...].

The binary XOR operation A xor B is identical to nonequivalence A≢B. A xor B can be implemented using AND and OR gates as

A xor B=(A ^ !B) v (!A ^ B)
(1)
=(A v B) ^ (!A v !B),
(2)

where  ^ denotes AND and  v denotes OR, and can be implemented using only NOT and NAND gates as

 A xor B=(A nand !B) nand (!A nand B)
(3)

(Simpson 1987), where  nand denotes NAND.

The binary XOR operator has the following truth table.

ABA xor B
TTF
TFT
FTT
FFF

For integers 0<=n<=m, the binomial coefficient (m; n) is odd iff the bitwise XOR satisfies m xor n=m-n. This gives a way to construct Pascal's triangle modulo 2.

For multiple arguments, XOR is defined to be true if an odd number of its arguments are true, and false otherwise. This definition is quite common in computer science, where XOR is usually thought of as addition modulo 2. In this context, it arises in polynomial algebra modulo 2, arithmetic circuits with a full adder, and in parity generating or checking. While this means that the multiargument "XOR" can no longer be thought of as "the exclusive OR" operation, this form is rarely used in mathematical logic and so does not cause very much confusion. The XOR operation is associative, so a xor (b xor c) is the same as (a xor b) xor c. Computation of the multiargument XOR requires evaluation of all its arguments to determine the truth value, and hence there is no "lazy" special evaluation form (as there is for AND and OR).

The ternary XOR operator therefore has the following truth table.

ABCA xor B xor C
TTTT
TTFF
TFTF
TFFT
FTTF
FTFT
FFTT
FFFF
BitXor

A bitwise version of XOR can also be defined that performs a bitwise XOR on the binary digits of two numbers x and y and then converts the resulting binary number back to decimal. Bitwise XOR is implemented in the Wolfram Language as BitXor[n1, n2, ...]. The illustration above plots the bitwise XOR of the array of numbers from -31 to 31 (Stewart 2000; Rangel-Mondragon; Wolfram 2002, p. 871).

For a positive integer n, the binary strings of length n form the vector space F_2^n over the finite field F_2 under componentwise addition modulo 2 (Menezes et al. 1996, pp. 79-81). Bitwise XOR is this vector addition, so it makes the strings into an Abelian group in which the all-zero string is the identity element and every element is its own inverse element. In particular,

x xor 0=x
(4)
x xor x=0
(5)
(x xor k) xor k=x xor (k xor k)=x.
(6)

Consequently, for a fixed string k, the map T_k(x)=x xor k is a permutation involution. If k!=0, then T_k has no fixed points and exchanges the strings in 2^(n-1) disjoint pairs {x,x xor k}. This reversibility requires one operand to be fixed or known. The output of a two-input XOR gate alone does not determine both inputs.

For binary strings x and y of equal length, the Hamming distance satisfies

 d_H(x,y)=w_H(x xor y),
(7)

where w_H denotes Hamming weight. Applying the same XOR mask to both strings preserves their Hamming distance.

In cryptography, a plaintext P and a key K of the same length can be combined to give the ciphertext C=P xor K, with decryption P=C xor K. A one-time pad uses a uniformly random key independent of the plaintext and never reuses it. It provides perfect secrecy, meaning that the ciphertext gives no information about the plaintext (Menezes et al. 1996, p. 192). Reversibility alone does not imply secrecy.


See also

AND, Aut, Binary Operator, Boolean Algebra, Connective, Hamming Distance, Logic, Munching Squares, NAND, NOR, NOT, One-Time Pad, OR, Pascal's Triangle, Symmetric Difference, Truth Table, XNOR

Portions of this entry contributed by Roger Germundsson

Explore with Wolfram|Alpha

References

Menezes, A. J.; van Oorschot, P. C.; and Vanstone, S. A. Handbook of Applied Cryptography. Boca Raton, FL: CRC Press, 1996. https://cacr.uwaterloo.ca/hac/.Numberphile. "Domino Addition." Featuring M. Parker. Apr. 4, 2014. https://www.youtube.com/watch?v=lNuPy-r1GuQ. Rangel-Mondragon, J. "A Catalog of Cellular Automata." https://library.wolfram.com/infocenter/MathSource/505/.Simpson, R. E. "The Exclusive OR (XOR) Gate." §12.5.6 in Introductory Electronics for Scientists and Engineers, 2nd ed. Boston, MA: Allyn and Bacon, pp. 550-554, 1987.Stewart, I. "A Fractal Guide to Tic-Tac-Toe." Sci. Amer. 283, 86-88, 2000.Wolfram, S. A New Kind of Science. Champaign, IL: Wolfram Media, p. 871, 2002.

Referenced on Wolfram|Alpha

XOR

Cite this as:

Weisstein, Eric W., with contributions by Roger Germundsson. "XOR." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/XOR.html

Subject classifications