Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

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

print(False ^ False)
print(False ^ True)
print(True ^ False)
print(True ^ True)

This outputs:

False
True
True
False

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:

print (6 ^ 5)

This outputs:

3

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

Python XOR (Exclusive OR)

We can directly apply Python XOR on bitstrings in the following manner.

x = 0b0101
y = 0b1010
print(x^y)

The output is:

15

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:

x = 0b0101
y = 0b1010
print(bin(x^y))

This yields:

0b1111

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

import operator

print(operator.xor(False,False))
print(operator.xor(False,True))
print(operator.xor(True,False))
print(operator.xor(True,True))
  
print(operator.xor(6,5))

This yields, as expected:

False
True
True
False
3

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:

print(False != False)
print(False != True)
print(True != False)
print(True != True)
  
print(6 != 5)

This yields:

False
True
True
False
True

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.

def myxor(v1,v2):
  return ((v1 and not v2) or (not v1 and v2))

print(myxor(False,False))
print(myxor(False,True))
print(myxor(True,False))
print(myxor(True,True))

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

x = 10; y = 12
print(x,y)
  
temp = x
x = y
y = temp
  
print(x,y)

The output is:

10 12
12 10

The Python XOR operator provides a way to swap these variables without an intermediate variable. Here is how that works.

print(x,y)

x = x^y
y = y^x
x = x^y
  
print(x,y)

The output is:

10 12
12 10

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.

Kodeclik sidebar newsletter

Join our mailing list

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

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.