How to use the XOR operator in Python

author bio image
Daniel Taylor
Head of Content at Here I Work
In this article, you'll dive into the world of bitwise operators, with focus on the XOR (^) operator, showing you how it works in Python with practical real-life examples. Let's dive in.

Operators serve as the fundamental components of any expression, whether it's in the realm of mathematics or logic. Without operators, articulating the specific operations you intend to execute would be very difficult.

Consider a scenario where you're piloting a light aircraft without a control yoke or joystick – directing its course would be virtually impossible. In a similar vein, the absence of operators would render it impossible for the compiler or interpreter of our code to discern the nature of the operations you aim to execute. This is where our friend the XOR operator comes in hand.

What is XOR?

XOR, also known as Exclusive OR is a bitwise operator (an operator which uses binary-namely 0s and 1s). The XOR operation returns 0 as an output if both inputs are the same (e.g. 0 & 0, or 1 & 1), and 1 if both inputs are different (e.g. 0 & 1, or 1 & 0). The visual representation of this is provided in the below truth table:

XOR Truth Table in Binary

Input 1Input 2Output

For further simplification, take two binary numbers 0101 and 1010, '⊕' is the mathematical symbol for the Exclusive OR operation so the truth table for 0101⊕1010 would be:

0101⊕1010 Truth Table

Input 1Input 2Output

The answer for 0101 ⊕ 1010, would be 1111 as no two inputs were the same (hence the name Exclusive OR).

How to use the XOR operator in Python?

Python is one of the easiest languages to learn about logic gates including the bitwise XOR operator due its beginner friendly syntax nature. You can practise this on websites such as or directly in your favourite code editor.

Method 1

Implementing XOR in Python is simple, the operator can easily be performed using the “^” character. An example is provided below:

#initialising the variables a & b for use with the XOR bitwise operator
#Printing the value using the (“^”) character to represent XOR.
print (a ^ b)
#This will print out 12

In the above example, Python first converts the values in the variables a & b into binary, then the bitwise operation is calculated and printed out.

Method 2

Another way to use XOR is via the xor() function using the operator module:

# Importing the operator module
import operator
x = 6
y = 7
print (operator.xor(x, y))
#This will print out 1.

Performing XOR on Booleans

Since XOR is a bitwise operator, it conducts a bit-by-bit comparison of the binary representations of the integers involved. It can therefore be used directly with Boolean values and Integers. False would be converted to 0 and True would be 1. For example:

# Initializing the boolean values
a = True
b = False
# Finding the XOR of boolean values using the ^ operator
group1 = a ^ a
group2 = a ^ b
group3 = b ^ a
group4 = b ^ b
# Printing the XOR value of every boolean combination
print(a, "XOR", a, "=", group1)
print(a, "XOR", b, "=", group2)
print(b, "XOR", a, "=", group3)
print(b, "XOR", b, "=", group4)

The above code will print out the following:

  • True XOR True = False
  • True XOR False = True
  • False XOR True = True
  • False XOR False = False

Use cases of XOR

1. Encryption

XOR works hand in hand with encryption, it was one of the very first ways in which encryption was used, many advanced encryption strategies today continue to follow the concepts derived from XOR encryption, though by itself it is a meagre form of data encryption.

Take an example of this at play; you have a friend named Dan, and only you and Dan know a key - lets say 8. If at some point in time you were to send him a digit such as 4 without anybody knowing what it was, the number can easily be encrypted using the XOR operator.

#initialising the variables for use with the bitwise operator
#Sending the encrypted value using the XOR (“^”) operator.
encrypted = digit ^ key
#The encrypted number will equal 12.

Once Dan has received the encrypted number, he would use the key to decode it back to the original value.

#Assigning values to the variables ‘encrypted’ & ‘key’.
encrypted = 12
key = 8
#Setting the decrypted value to the XOR of encrypted and key.
decrypted = encrypted ^ key
#The decrypted number will equal 10 so our original value is obtained.

2. Swapping two integers

Another use case of the XOR operator is to swap two numbers with each other. A real scenario in which this may come in hand is switching on the lights. Let's say we have two numbers 0 or 1, or in this case False and True, A is False (0) and B is True (1). We perform the swaps using the XOR operation in the following manner:

XOR Lights On or Off example:

# Assigning values to lightsOn and lightsOff, one of them must be true, in this case its lightsOn.
lightsOn = True
lightsOff = False
#Below we first perform the XOR operation on lightsOn, then on lightsOff, then once again on lightsOn. This performs the full swap.
lightsOn = lightsOn^lightsOff
lightsOff = lightsOn^lightsOff
lightsOn = lightsOn^lightsOff
#lightsOn will now be equal to False


  1. XOR, or Exclusive OR, is a powerful bitwise operator that you'll find quite fascinating. It works its magic by returning a value of '1' when one and only one of the operands happens to be '1,' which is essentially the same as saying 'True' in the world of logic.

  2. In the Python programming language, XOR is conveniently represented by the "^" symbol.

  3. You don't have to limit yourself to just the "^" symbol. Python offers you even more flexibility with the XOR operation. You can make use of the xor() function by importing the operator module.

  4. But wait, there's more! - Bonus Tip: XOR also allows you to check for equality. It returns 1 if the two operands are different and 0 if the two operands are the same. For e.g. if you had to compare two values you can XOR the two values and if it returns 1 then they are not equal and if it returns 0 then it is equal.

Additional Resources


Your #1 remote work community.

Copyright © 2023 - All right reserved