×

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand, and if they are different, the corresponding result bit is set to 1. If they are the same, the corresponding result bit is set to 0.

Here is an example of using the XOR operator in Python:

Example:

a = 5  # binary: 101
b = 3  # binary: 011
c = a ^ b  # binary: 110
print(c)  

Output:

 6

In this example, the binary representation of a is 101, and the binary representation of b is 011. When we perform the XOR operation, we compare each bit of a to the corresponding bit of b:

  • The first bit of a (the leftmost) is 1 and the first bit of b is 0, so the first bit of the result is 1.
  • The second bit of a is 0 and the second bit of b is 1, so the second bit of the result is 1.
  • The third bit of a is 1 and the third bit of b is 1, so the third bit of the result is 0.

So, the binary representation of the result is 110, which is equivalent to the decimal value 6. In python you can use the operator ^ for XOR operation.

The XOR operator in Python is a bitwise operator that compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. If they are the same, the corresponding result bit is set to 0.

The XOR operator can be used to perform various bit manipulation tasks such as flipping specific bits, setting or clearing bits, and more. For example, you can use the XOR operator to toggle specific bits in an integer without affecting the other bits.

Here is an example of using the XOR operator to toggle the third bit of a number:

x = 5  # binary: 101
mask = 1 << 2  # binary: 100
x = x ^ mask  # binary: 001
print(x)  

Output:

1

In this example, we first create a mask with the value 1 << 2, which is equivalent to shifting the binary number 1 to the left by 2 positions, resulting in binary 100. Then, we use the XOR operator to toggle the third bit of x by XORing it with the mask. This provides result in a new value of 1, which is the decimal representation of the binary number 001.

The XOR operator can be used on integer types in Python (int, long) as well as bytes and byte array. We can also use the XOR() function from the operator module to perform the same operation.

from operator import xor
print(xor(5, 3)) #6

Use of XOR in Python

The XOR operator in Python can be used in a variety of ways, some examples are as follows:

  • Bit manipulation: As mentioned before, the XOR operator can be used to perform various bit manipulation tasks such as flipping specific bits, setting or clearing bits, and more.
  • Encryption and decryption: The XOR operator can be used in simple encryption and decryption algorithms, such as the XOR Cipher. It can be used to encrypt plaintext by XORing it with a secret key, and then decrypting it by XORing it again with the same key.
  • Error detection and correction: The XOR operator can be used in error detection and correction codes such as the Hamming code, which uses XOR to detect and correct errors in data transmission.
  • Data compression: The XOR operator can be used in data compression algorithms, such as the LZ78 algorithm to compress data by identifying and encoding repeated patterns.
  • Boolean logic: The XOR operator can be used as a logical operator to implement the XOR function in Boolean logic. It returns true if one and only one of its operands is true.
  • Set membership test: XOR operator can be used to check if a number is present in a set or not. The number is XORed with all the numbers in the set and the result is checked if it is present in the set or not.

Related Topics

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

List Iteration in Python

In this tutorial, we will learn how to iterate list in Python. List in Python A list is an ordered group of values which includes several kinds of values.A list is a mutable...

3 minutes read.

Creating Tables using Python MySQL

In this article, we are going to learn how to create tables in databases using Python MySQL. Introduction to Tables: Generally, databases are used in order to store the information in the...

9 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Shallow Copy and Deep Copy in Python

Shallow Copy and Deep Copy in Python In this section, we will learn about the Shallow Copy and Deep Copy in the Python program. But before going through the topic, we...

6 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.