Thursday, December 26, 2024
Google search engine
HomeLanguagesGet the logical xor of two variables in Python

Get the logical xor of two variables in Python

The XOR or exclusive is a Boolean logic operation widely used in cryptography and generating parity bits for error checking and fault tolerance. The operation takes in two inputs and produces a single output. The operation is bitwise traditionally but could be performed logically as well. This article will teach you how to get the logical XOR of two variables in Python.

XOR ^ Operator between Two Integers

As XOR is a bitwise operator, it will compare bits of both integers bit by bit after converting them into binary numbers. The truth table for XOR (binary) is shown below:

A B A⊕B
1 1 0
0 1 1
1 0 1
0 0 0

The formula for XOR operation is:

XOR(A, B) = ( A .\overline{B}) + (B.\overline{A})

The XOR operation on two datatypes would be demonstrated using 

  • XOR on Integers
  • XOR on Strings

XOR on Integers

XOR operation is bitwise, and since integers have such representation, the operation could easily be done with such datatypes. Performing the XOR of two integers is trivial in Python, as the language offers an operator, especially for this purpose, namely a caret ^. The following code demonstrates the usage of a caret for performing the XOR of two integer variables.

Example 

Firstly two variables were initialized containing 10 and 27 integer values. Then the xor of the two variables is obtained using the caret operator. The result of the operation is displayed.

Python3




# First integer
a = 10
# Second integer
b = 27
 
# Performing the xor and storing the result in separate variable
xor = a ^ b
 
print(xor)


Output:

17

Time complexity: O(1)

Space complexity: O(1)

XOR on String

Since strings are a sequence, the datatype needs to be normalized for the operation to be performed on them. The XOR operator on two booleans is logical XOR, unlike on integers where it’s bitwise. Therefore, the strings would be converted to bool, and then the xor operation could be performed on them. But due to this, the result of the operation would be binary, i.e., it would result in either True or False (unlike xor of integers where resultant value is produced). The following example produces such effects:

Example 

Firstly two strings are defined. One of them is an empty string. Then the strings are converted to the boolean datatype, and the xor operation is performed on them. The result is displayed. 

Note: A few things to remember while performing the xor operation. 

  • The XOR should only be between homogeneous elements, i.e., their datatype should be the same. 
  • The bool of a string will result in True if the string is non-empty and False if the string is empty.

Python3




# First string
a = "Hello World!"
# Second string
b = ""
 
# Performing the xor operation
xor = bool(a) ^ bool(b)
 
print(xor)


Output:

True

Time complexity: O(n) 

Space complexity: O(n), where n is length of string

RELATED ARTICLES

Most Popular

Recent Comments