XOR in Python

XOR in Python | Top 3 Applications of XOR Operator in Python

Still Struggling to use XOR in Python? If yes, then check out the best uses of XOR operation in Python along with how to use it effectively.

Operators are keywords in python that are the essential building blocks of any program instruction. Operators can be Arithmetic(related to mathematical operations), Logical (help to implement logic) or as in our case Bitwise(those who work on the binary numbers).

Without operators, no practical program can be written. This article talks about one important Bitwise operator, the XOR operator.

Overview

What is XOR operator?

The XOR operator, also called the exclusive OR operator, is a highly useful bitwise operator in python.

The Bitwise operators or the Binary operators are operators that don’t work on the binary level. These operators perform specific operations for every bit of the operands, hence the name bitwise.

So what does the XOR operator do ? It reads every single bit from both operands and returns 0 if they are same, and 1 is they are different. The truth table for various cases of operands is given below.

ABResult(A 
000
011
101
110

In python, the XOR operator is denoted by the ‘^’ symbol e.g., 1 ^ 0 = 1

Example

Let us understand the working of the XOR operator in detail by using an example

Let us say we have two numbers A = 10 and B = 11 and we perform the XOR operation on them

The Binary for A is 1010

The Binary of B is 1100

Now the result C = A ^ B can be calculated according to the following table

ABC (A ^ B)
110
011
101
000
= 10= 11= 6

The answer is C = 0101 = 6

See also  How Long Does it Take To Learn JavaScript For A Beginner?

Basically we pick one bit from each operand, perform the XOR operation and repeat this process for every significant bit of both operands. What are significant bits, these are the bits that contain significant information.

Usage of XOR in Python

Let us implement the above example in python to see how it is implemented in code.

Example

# program to implement XOR in python

A = 0b1010

B = 0b1100

print(bin(A^B))

print(A^B)

Output

0b0110

6

Important Insight About XOR Operator

There is one interesting detail about the XOR operator that is used in various practical day-to-day applications. This makes XOR operator a very important and useful operator which any programmer should know.

“ If the result of XOR operation of two operands is again used to implement XOR operation with anyone of its initial operands, it gives the second operand .”

Example

A = 10

B = 11

C = A ^ B

print(f“A ^ B = {C}”)

print(f”the value of A is : {B^C}”)

print(f”the value of B is : {A^C}”)

Output

6

10

11

This does not seem like a big detail at first, but is used in many of the practical applications as discussed later in the article.

You May Also Like

Sum Function Python – How to Use Sum() Function in Python

Applications of XOR Operator

1. Swapping two variables without using a third variable

Swapping two variables is one of the basic and frequently done tasks in programming. While you may have seen swapping two variables using a third temporary variable, but using the XOR operator, we can do it we can do it without any other variable.

This is possible due to the property of XOR operation to return its operands, which is mentioned earlier in the article.

See also  Best 11 Applications of Python in Various Industries Around The World

Let us take an example

Suppose we have two variables A = 5 and B  = 6 and we want to swap them using XOR operation in python

The first step would be to find the XOR of A and B and storing it in one of the operands

A = A ^ B

So the value of A ^ B can  be calculated as

101 ^ 110 = 011 = 3

So the value of A is currently 3. The next step is to set B = A ^ B

This seems counterintuitive at first, but A now contains the value of 5 ^ 6, which applied in XOR operation with B(6) will give the other operand(in this case 5) and store it in B

B = A ^ B

Now B contains the value which was initially in A. Now, if we XOR A(which currently contains 101 ^ 110 or 5 ^ 6) with B(which is having 5 or 101) we will get the value 6(which was initially in B) and store it in A

A = A ^ B

The final code looks something like this

Example

# Program to swap two variables using XOR

A = 5

B = 6

print(“Before swapping : ”)

print(f“A = {A}”)

print(f“B = {B}”)

A = A ^ B

B = A ^ B

A = A ^ B

print(“After Swapping : ”)

print(f“A = {A}”)

print(f“B = {B}”)

Output

Before swapping : 

A = 5

B = 6

After swapping

A = 6

B = 5

2. Encryption

Encryption is the process of converting the data into a code format using a specific algorithm and a private key so that the data remains secure.

Data is a very important and private resource. Its theft can lead to many serious problems and may breach the users’ privacy. Hence it is important to protect the data by encrypting it.

In this way, even data gets stolen, the actual information cannot be decoded  from the encrypted code without the private key. Hence the data remains secure.

See also  C++ vs Java: Which One is Better to Choose for Your Future?

Encrypting data through XOR operations was one of the first encryption techniques used. It is a bit outdated but still an important technique to learn.

 Let us understand encryption using XOR in python using an example

Let the data be 47 which in binary is 101111 and we encrypt it using the key 9 which is 1001 in binary. So the data can be encrypted as 

101111 ^ 001001 = 100110

The data that will be stored is 100110 which is 54 in binary. Now to decrypt it, we again XOR it with the key to get the original data

100110 ^ 001001 = 101111

The code for this encryption will look something like this

Example

# program to show encryption using XOR operation

data = 47

key = 9

enc_data = data ^ key

print(f“The encrypted data is : {enc_data}”)

print(f“The decrypted data is : {enc_data ^ key}”)

Output

The encrypted data is : 54

The decrypted data is : 47

3. Checking Equality

The XOR operator returns 0 for the same bits and 1 for different bits. This fact can be used to check equality of two variables.

If the XOR of two variables is 0 then they are equal, otherwise they are unequal

Conclusion

This is all about XOR in Python and its usage. The XOR operator is a bitwise operator which works on individual bits. It return 0 if the two operands are same and 1 if they are different.

It is used for many practical applications such as checking equality of variables, swapping two variables, encryption of data etc. If you believe that we have missed any critical part of XOR in Python then comment down below. If you like this information then please share this article with your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *