What is XOR in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

What is XOR in Python?

In Python, XOR is a bitwise operator that is also known as Exclusive OR.
It is a logical operator which outputs 11 when either of the operands is 11 (one is 11 and the other one is 00), but both are not 11, and both are not 00.

The symbol for XOR in Python is '^' and in mathematics, its symbol is '⊕'.

Syntax

The XOR operator is placed between two numbers or boolean values.

How to perform the bitwise XOR in Python?

In Python, we can perform the bitwise XOR operation using the "^" symbol. The XOR operation can be used for different purposes; XOR of two integers, XOR of two booleans, Swapping two numbers using XOR, etc.

We can also use the xor() function using the operator module in Python.

XOR ^ Operator between 2 integers

As XOR is a bitwise operator, it will compare bits of both the integers bit by bit after converting them into binary numbers.

Truth table for XOR (binary)

ABA⊕B
110
011
101
000

Code

Output

In the above example, we are finding the XOR of 1515 and 3232, the result of which is 4747. The XOR operator first converted both the numbers in their binary form and then compared their bits bitwise.

To understand the working of XOR operation better, let us find the XOR of 15 and 32 by comparing their bits:

15=0b00111115 = 0b001111

32=0b10000032 = 0b100000

1532=0b0011110b100000\implies15 ⊕ 32 = 0b001111 ⊕ 0b100000

1532=0b101111\implies15 ⊕ 32 = 0b101111

The XOR of 1515 and 3232 is 0b1011110b101111, i.e., 4747.

Performing XOR on two booleans

XOR results TrueTrue when either of the operands are TrueTrue (one is TrueTrue and the other one is FalseFalse) but both are not TrueTrue and both are not FalseFalse.

Truth table for XOR (boolean)

ABA⊕B
FalseFalseFalse
TrueFalseTrue
FalseTrueTrue
TrueTrueFalse

Code

Output

In the above example, we are finding the XOR of the boolean values (TrueTrue and FalseFalse). Use this Online Python Compiler to compile your code.

Swapping two integers using XOR without a temporary variable

The XOR swap algorithm can swap the values of two integers without the use of a temporary variable, which is normally required in other swapping algorithms.

Swapping two integers using XOR

Code

Output

In the above program, we are swapping two integers without using a temporary variable with the help of the XOR operator.

XOR in Python using Operator Module

We can also use the XOR operation using the xor() function in the operator module. The xor() function can perform XOR operations on integers and booleans.

Code

Output

The above example uses the operator.xor() function with booleans and integers.

Want to become a sought-after Python developer? Our comprehensive Python course is the key to achieving your dreams.

Learn more about bitwise operators

The bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary numbers, and then the operations are performed bit by bit. The result is always in decimal format.
They can also be used with boolean values.

To learn more about bitwise operators, click here.

Conclusion

  • XOR is a bitwise operator that is short for Exclusive OR.
  • It returns 11 when one and only one of the operands are 11 or TrueTrue.
  • XOR in Python is denoted using the "^" symbol.
  • We can swap two integers without using a temporary variable with the help of the XOR operation.
  • XOR operation can also be used with the help of the xor() function by importing the operator module.