XOR, also known as exclusive OR, is a bitwise operator represented by ^
. It compares the binary representation of two numbers, and the result is 0
if the bits are the same or 1
if the bits are different. For example, the binary representation of 5
is 101
, and for 3
, it is 011
. When XOR is applied, 5 ^ 3
gives 110
, which equals 6
in decimal.
One important property of XOR is that any number XORed with itself results in 0
. For instance, 5 ^ 5 = 0
. Additionally, XORing a number with 0
returns the number itself, such as 5 ^ 0 = 5
. Another key property is that XOR is both commutative and associative, meaning the order in which you XOR multiple numbers does not matter. For example, a ^ b ^ c
is the same as c ^ a ^ b
.