How can I XOR two numbers without using the ^ operator?


#1

Question

How can I XOR two numbers without using the ^ operator?

Answer

It might help to write out the numbers on paper and do it as you would any other math. For bit strings of different lengths, simply write out the numbers and fill in the leftmost missing bits with 0s. For example:
0b10011001 ^ 0b1111
We’d write that out as follows:

  1001 1001
^ 0000 1111
-------------
  1001 0110

FAQ: Learn Python - Introduction to Bitwise Operators - This XOR That?