Account
Categories

XOR Operator (^)


Definition:

It is a bitwise operator, called the Bitwise XOR operator.

It works on the binary representation of numbers.

It takes two numbers (operands) and converts them into binary form in which each corresponding bit is compared.

If the bits are different, the result is 1 (True). If both bits are the same, the result is 0 (False).

Now you will clearly understand this with the help of the diagram.

a = 8   → 1000
b = 4   → 0100

        1000
      ^ 0100
      --------
        1100

Result = 12

Syntax:

result = operand1 ^ operand2

Example:

a = 8   # binary: 1000
b = 4   # binary: 0100

result = a ^ b

print(result)

Output:

Explanation:

  1000 (8)
^ 0100 (4)
------------
  1100 = 12

Exercise

Use Bitwise XOR operator with values 9 and 5 and print the result using Python.