Kodeclik Blog


Python’s XOR Operator

The XOR operator is a boolean operator that returns two only if its inputs are different and false otherwise. It is denoted by the carat symbol (^). Consider the XOR applied on the truth table of two inputs (four possibilities):
This outputs:
As you can see only the second and third print statements are performing XOR on two different inputs. The other two print statements (first and fourth) have the inputs the same and thus return the XOR result to be False.

Python XOR on Integers

The XOR operator can be applied on integers as well. Internally when you invoke XOR on integers, Python converts the integers to their boolean representations and then conducts a bitwise XOR on these representations. The result is then converted back to integer notation.
For instance, consider:
This outputs:
How does this work? First, convert each of the two input integers (6 and 5) into binary notation. 6 can be represented as “110”. 5 can be represented as: “101”. Now align these two numbers and conduct a bitwise comparison using the XOR operator. This gives “011” which when converted back to decimal yields 3.

Python XOR on bit strings

We can directly apply Python XOR on bitstrings in the following manner.
The output is:
Because each location has a zero and 1 from the two inputs and the result is “1111” which is 15 in decimal. If you desire to retain the answer in bitstring notation, you can use the bin() function:
This yields:

Python XOR using Operator Module

Another way to do XOR is to import the operator module in Python. Then we can redo the same examples above in the following manner
This yields, as expected:

XOR is the same as != for boolean values

Since XOR is comparing bitwise values and seeing if they are different you can get the same effect using the != comparison operator.
Let us try that:
This yields:
Wow - what happened? The != comparison operator works similar to the XOR for boolean values but fails for the integers. This is because when integers are compared using != this operator checks whether the inputs are not equal and thus returns True rather than the bitwise XOR we were expecting.

Implementing XOR

To understand more about XOR, we can implement an XOR function ourselves. Here is a functional implementation of XOR.
As you can see myxor provides the same result as the in-built XOR operator (“^”). Note how the definition of myxor is specified. We check for two conditions (either v1 is True and v2 is False, or v1 is False and v2 is True).

Swapping Integers using XOR

Remember how we are used to swapping integers using an intermediate variable. Consider the following program that swaps the values of variables x and y using a third, temporary, variable called “temp”:
The output is:
The Python XOR operator provides a way to swap these variables without an intermediate variable. Here is how that works.
The output is:
Why does this work? In the first XOR, we are over-writing the x variable by recording the positions where its value is different from y (call it a “mask”). Then when we XOR the mask with y, y gets replaced with x. By XOR-ing the mask again with y, we can retrieve the old value of y which is then assigned to x. This is how the two variables get swapped by careful book-keeping of the bit positions.
Note that this approach works only for integers.
We have learnt the very important Python XOR operation and how it can be applied to bit strings as well as to integers and boolean variables. Let us know where you find it useful! You can learn more about Python's logical operators in our post dedicated to Python boolean operators. Also if you like learning innovative ways to swap variables, checkout our blogpost on how to swap variables in Javascript.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.