XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are identical XOR outputs as 0 and when two bits are different then XOR outputs as 1. XOR can even be used on booleans.

XOR is mainly used in situations where we don’t want two conditions to be true simultaneously. In this tutorial, we will look explore multiple ways to perform XOR (exclusive OR) operations in Python with examples.

Bitwise Operator

Bitwise operators in Python are also called binary operators, and it is mainly used to perform Bitwise calculations on integers, the integers are first converted into binary, and later the operations are performed bit by bit.

Python XOR Operator

Let’s take a look at using the XOR ^ Operator between 2 integers. When we perform XOR between 2 integers, the operator returns the integer as output.

a=  5  #0101
b = 3  #0011

result	= (a ^ b) #0110

print(result)

# Output
# 6 (0110)

Let’s take a look at using XOR on two booleans. In the case of boolean, the true is treated as 1, and the false is treated as 0. Thus the output returned will be either true or false.

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

Output

False
True
True
False

XOR using Operator Module

We can even achieve XOR using the built-in operator module in Python. The operator module has a xor() function, which can perform an XOR operation on integers and booleans, as shown below.

import operator

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

Output

6
False
True
True
False
2 comments
  1. x = 5
    x |= 3
    print(x)
    # answer is: 7

    Hi
    please explain for me why the answer is 7 in above line of codes in Python?
    I do not understand.
    thank you

    1. Hello John,

      The example that you have provided is for In-Place Bitwise OR operator. The OR operator is represented in Python using the |=

      However, the topic is on the XOR operator also called as (eXclusive OR) and it is represented using the In-place bitwise operator ^=.

      Below is the code using the XOR which results the output to 6. So if you would like to use the XOR you need to modify your code as mentioned below.

      a = 5
      a ^= 3
      print(a)

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Ternary Operator

Python Ternary Operator

Python Ternary operators also called conditional expressions, are operators that evaluate something based on a binary condition. Ternary operators provide a shorthand way to write conditional statements, which makes the…
View Post
How To Reverse A String In Python

How to Reverse a String in Python

Table of Contents Hide Reverse a string in PythonUsing extended slice operatorUsing for loopUsing while loopUsing recursionUsing reversed() method Python has many functions for string manipulation. However, the Python string…
View Post
Remove Character From String Python

Remove Character From String Python

Table of Contents Hide Python Remove a Character from a StringPython Remove Character from String using replace()Remove a single character from a stringRemove all occurrences of a character from a…
View Post
Convert String To Float In Python

Convert String to Float in Python

Table of Contents Hide Convert string to float in Pythonfloat() functionExample 1 – Convert string to floatExample 2 – Default float conversion without any input parameterExample 3 – Convert a…
View Post