Trusted answers to developer questions

What is the XOR bitwise operator?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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 (>>)
  • The following operator needs one operand.

    • 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:

  1. (5 ^ 3)

    • 5 in binary = (101)
    • 3 in binary = (011)
    • (101)^(011) = (110)
    • (110) in decimal = 6
    • Thus, 5^3 = 6.
  2. (10 ^ 2)

    • 10 in binary = (1010)
    • 2 in binary = (010)
    • (1010)^(010) = (1000)
    • (1000) in decimal = 8
    • Thus, 10^2 = 8.
1 of 5

XOR properties

  1. XOR is commutative.

    • This means, a^b = b^a.
  2. XOR is associative.

    • This means, a^(b^c) = (a^b)^c = (a^c)^b.
  3. XOR is self-inverse.

    • This means, any number XOR’ed with itself evaluates to 0.

    • a^a = 0.

  4. 0 is the identity element for XOR.

    • This means, any number XOR’ed with 0 remains unchanged.
    • a^0 = a.

RELATED TAGS

xor
bitwise operator
bitwise
Did you find this helpful?