While programming in Python, the βXORβ operator is utilized in several tasks. For instance, to detect errors or issues in data transmission with the help of checksum, apply encryption on messages, or retrieve certain bits from a number. More specifically, it is used for comparing two sets of data or strings and determining if they are similar or have some differences.
This blog will cover the following aspects:
- What is XOR in Python?
- How Does XOR Work in Python?
- XOR Truth Table
- How to XOR Two Strings in Python?
- Relevant Case Scenarios
What is XOR in Python?
In Python, βXORβ or βExclusive ORβ is an operator that compares two binary numbers bitwise. This operator returns β0β if both bits are the same. Else, it outputs β1β in case both values are different. Python’s built-in XOR operator permits us to logically combine two values with the help of the Exclusive OR operation.
How Does XOR Work in Python?
The XOR operator is represented as β^β in Python, and it follows the given syntax:
Here, βxβ and βyβ indicate the binary numbers, β^β represents the XOR operator, and βxor_numberβ stores the resultant Boolean value.
XOR Truth Table
To know more about the working of the XOR operator, look at the given table:
x | y | x ^ y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
How to XOR Two Strings in Python?
As XOR is a bitwise operator, it can only operate on integers. However, to perform the XOR operation between two strings, it is required to convert them into their respective ASCII/Unicode values using the βord()β function and then apply the β^β operator.
Three major case scenarios exist when considering the XOR operator implication on string values:
- Case 1: Both strings contain binary values.
- Case 2: Both strings comprise characters.
- Case: 3: One string is based on integers/binary values, and the second contains characters.
Letβs practically demonstrate the stated cases!
Case 1: Apply XOR on Two Strings Having Binary Values
To apply the XOR operator on two strings having binary values, firstly initialize them as follows:
string2 = "1001010"
Now, add the following lines:
print(result)
According to the given code:
- Invoke the βzip()β function with the βforβ loop to create pairs of characters βaβ, βbβ, from the βstring1β and βstring2β.
- Each generated pair is then passed to the βord()β function to convert them to their respective ASCII values.
- β^β performs the XOR operation on the converted ASCII/Unicode values and appends them to the list.
- Lastly, the resultant value is stored in the βresultβ variable and displayed using the βprint()β function:
It can be observed that for each same index bit of both strings, the XOR operator outputs β0β, and for different values, β1β is returned.
Case 2: Apply XOR on Two Strings Having Characters
Now, the XOR operator is applied on two strings having the following character values:
string2 = "ten"
result = [(ord(a) ^ ord(b)) for a,b in zip(string1, string2)]
print(result)
In this case, the βord()β function returns the ASCII value for each generated and pass them to β^β to perform the XOR operation:
Another simplest way to remember the working of this case is when the βord()β function returns the ASCII value for each pair, the XOR operator outputs their subtracted value.
For example:
- The first character of both strings is βtβ, whose ASCII value is β116β, so the XOR operator returned 0 (116-116).
- The second character of the first string is βwβ with the ASCII value β119β, and for the second string, the βeβ character exists on the same index having ASCII value β101β. Therefore, the XOR operator outputs 18 (119-101).
- For the last bit, the first string contains βoβ with ASCII value β111,β and the second string comprises βnβ having β110β ASCII value. As a result, the XOR operator returned 1 (111-110).
Case 3: Apply XOR on Two When One String Contains Characters, and Other is Based on Integers/Binary Values
Last case in the ongoing discussion is when one string contains characters, such as βtwoβ, and the other one comprises integers or binary values, like β111β:
string2 = "111"
result = [chr(ord(a) ^ ord(b)) for a,b in zip(string1, string2)]
print(result);
Here, the code works similarly to the second case. Except that the returned ASCII values have been converted to the respective characters using the βchr()β Python function:
The returned list represents the characters of the corresponding ASCII values.
Conclusion
In Python, XOR β^β returns β0β if both bits are the same. Else, it outputs β1β. However, both strings comprise characters, values are converted into their ASCII representation using the βord()β function, and then the XOR operator is applied. The same operation is performed when both strings contain different data types, like characters and integers. Only the differentiation is to invoke the βchr()β function to get the resultant value in characters. This blog demonstrated how to XOR two strings in Python.