Exclusive or
From Free net encyclopedia
- This article is about XOR in the logical sense. For the electronic XOR gate see XOR gate. For other uses, see XOR (disambiguation).
Exclusive or (usual symbol XOR occasionally EOR), which is sometimes called exclusive disjunction, is a logical operator that results in true if one of the operands, but not both of them, is true. Image:XOR.jpg
Contents |
Definition
In English and other languages, the treatment of the word or requires a little care. The exclusive disjunction of propositions A and B means A or B, but not both, as in "you can follow the rules or be disqualified" -- the subject can only choose one of the operands. In logic, the word 'or' usually means the other, inclusive, disjunction. (Some languages, such as Latin, have different words for these different types of "or".)
More formally exclusive disjunction is a logical operator. The operation yields the result TRUE when one, and only one, of its operands is TRUE. The exclusive disjunction of propositions A and B is usually called A xor B, where "xor" stands for "exclusive or" and is pronounced "eks-or" or "zor".
For two inputs A and B, the truth table of the operator is as follows.
A | B | A xor B |
---|---|---|
F | F | F |
F | T | T |
T | F | T |
T | T | F |
It can be deduced from this table that
- (A xor B) ⇔ (A and not B) or (not A and B) ⇔ (A or B) and (not A or not B) ⇔ (A or B) and not (A and B)
Symbol
The mathematical symbol for exclusive disjunction varies in the literature. In addition to the abbreviation "xor", one may see
- the symbol Image:Xor.png.
- a plus sign ("+") or a plus sign that is modified in some way, such as being put inside a circle ("<math>\oplus</math>, ⊕"); this is used because exclusive disjunction corresponds to addition modulo 2 (where 0+0 = 1+1 = 0 , and 0+1 = 1+0 = 1). The HTML entity for the circled plus character is "⊕".
- a "v" that is modified in some way, such as being underlined ("∨", or "⊻": Template:U "XOR"); this is used because exclusive disjunction is a modification of ordinary (inclusive) disjunction, which is typically denoted by a vee ("∨").
- In the C programming language and Java programming language, a caret ("^") is used as both a logical and bitwise XOR operator. This is not used outside of programming contexts because it would create confusion; in logic, the caret is often used for the and operation and on calculators it usually denotes exponentiation.
Similarly, different textual notations are used, including "orr" (modelled on iff, of which it is the negative).
Definition using symbols
Using the symbols above one can write the XOR as:
A ⊻ B = (A ∧ !B) ∨ (!A ∧ B) (this is the definition)
After some calculation, one can also derive that XOR can be written as:
A ⊻ B = !(A ∧ B) ∧ (A ∨ B)
This representation of XOR is very powerful, because it has only one ! operation and small number of ∧ and ∨ operations. When constructing a circuit or network that works as a XOR, you will find this expression handy. The proof of this expression is given below:
Proof:
A ⊻ B = (A ∧ !B) ∨ (!A ∧ B)
= {(A ∧ !B) ∨ !A} ∧ {(A ∧ !B) ∨ B}
= {(A ∨ !A) ∧ (!B ∨ !A)} ∧ {(A ∨ B) ∧ (!B ∨ B)}
= (!A ∨ !B) ∧ (A ∨ B)
= !(A ∧ B) ∧ (A ∨ B)
It is sometimes useful to write XOR as:
A ⊻ B = !( (A ∧ B) ∨ (!A ∧ !B) )
This result is simply De Morgan's theorem applied (twice) to the fourth line of the above proof.
Associativity and commutativity
For successive xor operations, xor can be applied to the first two inputs and then the result can be xor'ed with each subsequent input, or in any other order:
- (A xor B xor C xor D) ⇔ (((A xor B) xor C) xor D)
Because xor is associative, the order of the inputs does not matter: the same result will be obtained regardless of association:
- (A xor (B xor C) ) = ( (A xor B) xor C)
The operator xor is also commutative and therefore the order of the operands is not important:
- A xor B ⇔ B xor A
List of Some Properties
This section uses the symbols:
A ⊕ B = A ⊻ B = A xor B
A * B = A ∧ B = A and B
A + B = A ∨ B = A or B
A ⊕ !A = 1
A ⊕ A = 0
A ⊕ 1 = !A
A ⊕ 0 = A
A ⊕ B = B ⊕ A
A ⊕ B ⊕ A = B
A ⊕ (B ⊕ C) = (A ⊕ B) ⊕ C
A ⊕ B = !A ⊕ !B
!(A ⊕ B) = (!A) ⊕ B = A ⊕ (!B)
A ⊕ (!A * B) = A + B
A ⊕ (A * !B) = A * B
A ⊕ (A + B) = !A * B
(!A) ⊕ (A + !B) = A + B
A * (A ⊕ !B) = A * B
A + (A ⊕ B) = A + B
Bitwise operation
Exclusive disjunction is often used for bitwise operations. Examples:
- 1 xor 1 = 0
- 1 xor 0 = 1
- 1110 xor 1001 = 0111 (this is equivalent to addition without carry)
In computer science
In computer science, exclusive disjunction is commonly referred to as "exclusive-or" or "xor". It has several uses:
- It tells whether two bits are unequal.
- It is an optional bit-flipper (the deciding input chooses whether to invert the data input).
- It tells whether there are an odd number of 1 bits (A ⊕ B ⊕ C ⊕ D ⊕ E is true iff an odd number of the variables are true).
On some computer architectures, it is more efficient to store a zero in a register by xor-ing the register with itself (bits xor-ed with themselves are always zero) instead of loading and storing the value zero. Because 'xor' is a more complex logical function than 'or' and 'and', its neural network requires an additional processing layer.
Exclusive-or is sometimes used as a simple mixing function in cryptography, for example, with one-time pad or Feistel network systems.
XOR is used in RAID 3–6 for creating parity information. For example, RAID can "back up" bytes 10011100
and 01101100
from two hard drives by XORing (11110000
) and writing to a third drive. Under this method, if any one of the three hard drives are lost, the lost byte can be re-created by XORing bytes from the remaining drives. If the drive containing 01101100
is lost, 10011100
and 11110000
can be XORed to recover the lost byte.
XOR is also used to detect an overflow in the result of a signed binary arithmetic operation. If the leftmost retained bit of the result is not the same as the infinite number of digits to the left, then that means overflow occurred. XORing those two bits will give a "one" if there is an overflow.
More than two inputs
The case of a three, or more, input XOR operation tends to be an ill-considered case. An XOR operation with three simultaneous inputs is not so easily expressed in most popular programming languages. When expressed as successive binary operations the result is a simple parity, giving a TRUE result for any odd number of TRUE inputs.
Image:Three ones EORed.png When taken as a multi-input black box whose operation follows the formal definition and yields the result TRUE when one, and only one, of its inputs is TRUE, the behaviour is inconsistent with the successive binary mode. For example; in the case where three inputs are all TRUE (as shown on the right, in the IEC symbology to emphasise the definition) the result would have to be FALSE because it does not meet the condition that "only one of its inputs is TRUE".
See also
Template:Col-begin Template:Col-3
- Applications
- In logic
- In mathematics
- Other gates
Template:Col-endde:Kontravalenz fr:OU exclusif it:Disgiunzione esclusiva nl:Exclusieve disjunctie ja:排他的論理和 no:Eksklusiv disjunksjon pl:Alternatywa wykluczająca fi:XOR tr:XOR kapısı