The XOR operator, also known as the exclusive or operator, is a bitwise operator used in Python to perform logical operations on binary values. This article explains how to use the XOR operator in Python with code examples.
We will look to explore multiple ways to perform XOR (exclusive OR) operations in Python with examples. which include XOR operations on binary values, XOR encryption and decryption, and XOR comparison of strings.
XOR is typically used when we don’t want two conditions to be true at the same time.
What is XOR Operator
Python’s “exclusive or” (XOR) operator compares two binary values bitwise; when two bits are the same, XOR returns 0; when two bits are different, XOR returns 1. XOR
can be applied to booleans as well.
Let’s take a look at using XOR on two booleans. The true
is treated as 1
, and the false
is treated as 0
.
Syntax of the XOR operator in Python
The XOR operator in Python has the following syntax:
a ^ b
Where a
and b
are the binary numbers to be compared.
XOR Example
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.
Here’s an example of how the XOR operator works in Python:
a= 5 #0101 b = 3 #0011 result = (a ^ b) #0110 print(result)
In this example, we use the XOR operator to compare the binary values of a
and b
. The result is a new binary value stored in c
, where each bit is set to 1 only if the corresponding bits in a
and b
are different.
Output:
6 (0110)
Example of xOR Python with Boolean:
print(True ^ True) print(True ^ False) print(False ^ True) print(False ^ False)
Output:
False True True False
Python XOR truth table :
A | B | A⊕B |
---|---|---|
1 | 1 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
0 | 0 | 0 |
XOR Encryption and Decryption
XOR can also be used for encryption and decryption purposes. In XOR encryption, a plaintext message is XORed with a secret key to create ciphertext. To decrypt the ciphertext, the same key is XORed with the ciphertext to obtain the original plaintext message.
Here’s an example of how XOR encryption and decryption works in Python:
plaintext = "secret string" key = "secret key" # encryption ciphertext = ''.join(chr(ord(x) ^ ord(y)) for x, y in zip(plaintext, key)) print(ciphertext) # decryption decryptedtext = ''.join(chr(ord(x) ^ ord(y)) for x, y in zip(ciphertext, key)) print(decryptedtext)
we use XOR to encrypt and decrypt a plaintext message. The ord
function is used to get the ASCII code of each character, and the chr
function is used to convert the ASCII code back to its corresponding character. The zip
function is used to pair the characters of the plaintext and key, and the XOR operator is used to create the ciphertext and decrypted text.
XOR in Python
Even XOR can be accomplished using Python’s built-in operator module. A function called xor() in the operator module allows for the XOR operation on integers and booleans.
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
XOR for String Comparison
XOR can also be used for string comparison in Python. Here’s an example of how this can be done:
string1 = "adam" string2 = "eve" result = ''.join(chr(ord(x) ^ ord(y)) for x, y in zip(string1, string2)) if result == '\binary-value\': print("The strings are equal") else: print("The strings are not equal")
In this example, we use XOR to compare two strings, string1
and string2
. The XOR operator is applied to the corresponding characters in each string using the zip
function. If the result of the XOR operation is equal to the binary value '\binary-value
\'
, then the strings are considered equal.
Conclusions:
This article is a helpful guide for anyone looking to learn about XOR in Python and how to use it effectively in their code.