Last Updated on March 10, 2023 by Prepbytes
The XOR operation is widely used in digital logic circuits, cryptography, network security, and programming languages like Python. XOR operation in Python is also known as Exclusive OR. Let’s deep dive and know more about the XOR Operation in Python.
What is XOR Operation?
XOR (exclusive OR) is a logical operation that compares two binary digits and returns a 1 only if the two digits are different. The XOR operation returns 0 if both bits are the same. In other words, XOR returns 1 when either of the binary digits is 1, but not both. This XOR Operation is represented as
output = input1 ⊕ input2
Truth Table for XOR Operation
The Truth Table given below shows the output of XOR Operation for different combinations of input.
For example, consider the two binary digits, 1 (001) and 0 (000). When we apply XOR to these digits, we get a result of 1 (001), as only one digit is 1.
Similarly, when we apply XOR to the digits 1 (001) and 1 (001), we get a result of 0 (000), as both digits are the same.
XOR Operation
In Python, the XOR operation is performed using the caret (^) symbol.
Syntax of the XOR Operation in Python
The syntax of XOR in python is
result = num1 ^ num2
Here, num1 and num2 are the binary numbers we want to perform the XOR operation on, and the result is the output of the XOR operation
Examples of XOR Operation in Python
Let’s see examples of XOR operation in Python:
Example 1 of XOR Operation in Python: Integers
The code for XOR Operation between Two Integers in Python is given below.
Code:
a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 # XOR operation result = a ^ b # 49 = 0011 0001 print("The result of XOR operation is:", result)
Output:
The result of XOR operation is: 49
Explanation:
In this example, we have assigned the values 60 and 13 to variables a and b, respectively. Then, we performed the XOR operation on these variables using the caret (^) symbol and assigned the result to variable ‘result’. Finally, we have printed the result of the XOR operation using the print() function.
Example 2 of XOR Operation using Booleans in Python
The given code demonstrates the usage of XOR Operation in Python on Boolean Values.
Code:
# Defining two boolean values a = True b = False # Using XOR Operation c = a ^ b print(c)
Output:
True
Explanation:
In this example, we have two boolean values a and b. We perform XOR operation between them using the ^ operator, which returns True if the operands are different and False if they are the same. In this case, since a is True and b is False, the result is True.
Example 3 of XOR Operation using Lists in Python
This code shows how we can use the XOR Operation on the numbers defined in form of a list.
Code:
a = [1, 2, 3, 4] b = [3, 2, 5, 6] # XORing the two lists c = [x ^ y for x, y in zip(a, b)] print(c)
Output:
[2, 0, 6, 2]
Explanation:
In this example, we have two lists of integers a and b. We perform the XOR operation between corresponding integers in both lists using list comprehension and the zip function. The resulting list c contains the result of the XOR operation between each corresponding pair of integers.
Use Cases of XOR Operation in Python
XOR operation has various use cases in programming. Some of the most common use cases are
-
Data Encryption:
XOR operation is used for data encryption, where a binary key is XOR-ed with the plaintext to produce the ciphertext. This is called one-time pad encryption. -
Error Detection & Correction:
XOR operation is used for error detection and correction in communication systems. In this method, the sender adds a parity bit to the message by XOR-ing all the data bits. The receiver performs the same XOR operation on the received message and compares the result with the parity bit. If the result is the same, it means that the message has been received correctly. Otherwise, it means that there is an error in the message -
Bit Masking:
XOR operation is used for bit masking, where a binary value is XOR-ed with a mask to selectively enable or disable certain bits in the value. For example, suppose we want to toggle the value of the third bit of a binary number. We can do this using the XOR operation with a mask that has the third bit set to 1 and all other bits set to 0.
Conclusion
In this article, we have learned about the XOR Operation in Python with the help of examples. XOR Operation is a simple yet powerful operation that is widely used in programming languages like Python. It is used for data encryption, error detection and correction, bit masking, and generating random numbers. Hope you have understood the concept well.
Frequently Asked Questions (FAQs)
Here are some Frequently Asked Questions on XOR Operation in Python.
Ques 1. Can the XOR operator be used on non-integer types in Python?
Ans. Yes, the XOR operator can be used on non-integer types in Python as long as the operands can be converted to binary. For example, we can use the XOR operator on strings that represent binary values.
Ques 2. How does the XOR operator differ from the OR and AND operators in Python?
Ans. The OR and AND operators in Python return a new binary value where each bit is set to 1 if the corresponding bits of the two operands are both 1 (for AND) or at least one of the operands is 1 (for OR). The XOR operator, on the other hand, returns a new binary value where each bit is set to 1 if the corresponding bits of the two operands are different, and 0 otherwise.
Ques 3. How can we use the XOR operator in Python to check if two numbers are different?
Ans. We can use the XOR operator to check if two numbers are different as follows: a ^ b != 0.
Ques 4. How can we use the XOR operator in Python to toggle a bit?
Ans. We can use the XOR operator to toggle a bit as follows: a ^= 1.