One thing that other answers don't mention here is XOR with negative numbers -
a | b | a ^ b
----|-----|------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
While you could easily understand how XOR will work using the above functional table, it doesn't tell how it will work on negative numbers.
How XOR works with Negative Numbers :
Since this question is also tagged as python, I will be answering it with that in mind. The XOR ( ^ ) is an logical operator that will return 1 when the bits are different and 0 elsewhere.
A negative number is stored in binary as two's complement. In 2's complement, The leftmost bit position is reserved for the sign of the value (positive or negative) and doesn't contribute towards the value of number.
In, Python, negative numbers are written with a leading one instead of a leading
zero. So if you are using only 8 bits for your two's-complement
numbers, then you treat patterns from 00000000 to 01111111 as the
whole numbers from 0 to 127, and reserve 1xxxxxxx for writing negative
numbers.
With that in mind, lets understand how XOR works on negative number with an example. Lets consider the expression - ( -5 ^ -3 ) .
The binary representation of -5 can be considered as 1000...101 and
Binary representation of -3 can be considered as 1000...011.
Here, ... denotes all 0s, the number of which depends on bits used for representation (32-bit, 64-bit, etc). The 1 at the MSB ( Most Significant Bit ) denotes that the number represented by the binary representation is negative. The XOR operation will be done on all bits as usual.
Since, the MSB becomes 0 after the XOR operation, so the resultant number we get is a positive number. Similarly, for all negative numbers, we consider their representation in binary format using 2's complement (one of most commonly used) and do simple XOR on their binary representation.
The MSB bit of result will denote the sign and the rest of the bits will denote the value of the final result.
The following table could be useful in determining the sign of result.
a | b | a ^ b
------|-------|------
+ | + | +
+ | - | -
- | + | -
- | - | +
The basic rules of XOR remains same for negative XOR operations as well, but how the operation really works in negative numbers could be useful for someone someday 🙂.
Another application for XOR is in circuits. It is used to sum bits.
When you look at a truth table:
x | y | x^y
---|---|-----
0 | 0 | 0 // 0 plus 0 = 0
0 | 1 | 1 // 0 plus 1 = 1
1 | 0 | 1 // 1 plus 0 = 1
1 | 1 | 0 // 1 plus 1 = 0 ; binary math with 1 bit
You can notice that the result of XOR is x added with y, without keeping track of the carry bit, the carry bit is obtained from the AND between x and y.
x^y // is actually ~xy + ~yx
// Which is the (negated x ANDed with y) OR ( negated y ANDed with x ).
The (^) XOR operator generates 1 when it is applied on two different bits (0 and 1). It generates 0 when it is applied on two same bits (0 and 0 or 1 and 1).