What is the XOR bitwise operator?
What are bitwise operations?
We can perform Bitwise operations on one or two numbers on their binary level.
-
The following operators need two operands:
- AND (
&) - OR (
|) - XOR (
^) - Left shift (
<<) - Right shift (
>>)
- AND (
-
The following operator needs one operand.
- 1’s complement (
~)
- 1’s complement (
XOR
XOR (^), or eXclusive OR, is a bitwise operator that returns true (1) for odd frequencies of 1.
The XOR truth table is as follows:
- 1
^1 = 0 - 1
^0 = 1 - 0
^1 = 1 - 0
^0 = 0
Some examples of XOR are as follows:
-
(5
^3)- 5 in binary = (101)
- 3 in binary = (011)
- (101)
^(011) = (110) - (110) in decimal = 6
- Thus, 5
^3 = 6.
-
(10
^2)- 10 in binary = (1010)
- 2 in binary = (010)
- (1010)
^(010) = (1000) - (1000) in decimal = 8
- Thus, 10
^2 = 8.
This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.
XOR properties
-
XOR is commutative.
- This means,
a^b=b^a.
- This means,
-
XOR is associative.
- This means,
a^(b^c) = (a^b)^c= (a^c)^b.
- This means,
-
XOR is self-inverse.
-
This means, any number XOR’ed with itself evaluates to 0.
-
a^a= 0.
-
-
0 is the identity element for XOR.
- This means, any number XOR’ed with 0 remains unchanged.
a^0 = a.