What mathematical operation does XOR perform?
XOR is a binary operation, it stands for "exclusive or", that is to say the resulting bit evaluates to one if only exactly one of the bits is set.
This is its function table:
a | b | a ^ b
--|---|------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
This operation is performed between every two corresponding bits of a number.
Example: 7 ^ 10
In binary: 0111 ^ 1010
0111
^ 1010
======
1101 = 13
Properties: The operation is commutative, associative and self-inverse.
It is also the same as addition modulo 2.
-
2THANK YOU!! I wish I had asked earlier - nearly drove myself mad this morning. Thank you for ensuring me a much more pleasant afternoon!! – Hilary Park Jan 25 '13 at 17:04
-
Man, that is really so very simple! I'm working through understanding hash algorithms more and this is a very constant operation among many of them. – Mike Perrenoud Dec 4 '13 at 18:11
-
2
^
is the Python bitwise XOR operator. It is how you spell XOR
in python:
>>> 0 ^ 0
0
>>> 0 ^ 1
1
>>> 1 ^ 0
1
>>> 1 ^ 1
0
XOR stands for exclusive OR. It is used in cryptography because it let's you 'flip' the bits using a mask in a reversable operation:
>>> 10 ^ 5
15
>>> 15 ^ 5
10
where 5
is the mask; (input XOR mask) XOR mask gives you the input again.
-
5The truth table speaks the truth - and in conversation I usually describe XOR (eXclusive-OR, sometimes also known as EOR) as 'either A or B, but not both or neither'. – Eight-Bit Guru Jan 25 '13 at 17:00
-
1Thank you!!! You and the other respondent have surely saved my sanity for the day --- I simply could NOT figure it out and it was so un-google-able. – Hilary Park Jan 25 '13 at 17:04