We can perform Bitwise operations on one or two numbers on their binary level.
The following operators need two operands:
&
)|
)^
)<<
)>>
)The following operator needs one operand.
~
)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 = 0^
0 = 1^
1 = 1^
0 = 0Some examples of XOR are as follows:
(5 ^
3)
^
(011) = (110)^
3 = 6.(10 ^
2)
^
(010) = (1000)^
2 = 8.XOR is commutative.
a
^b
= b
^a
.XOR is associative.
a
^(b
^c
) = (a
^b
)^c
= (a
^c
)^b
.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.
a
^0 = a.