# XOR Operator with Boolean & Integer in Python

Updated Oct 09, 2023

Operators are the source to perform any operation on variables and values in programming. They are the special symbols that execute arithmetic and logical calculations and have different types. Operands are the values on which the operator works. This article is a walkthrough of one of the common bitwise operators in Python to perform bitwise operations on two operands (values).

## What is Bitwise Operator?

Users can use bitwise operators to perform bitwise operations on values. These operators first convert the values into binary format and then execute the operations bit by bit. It gave the name bitwise operators.

One can use bitwise operators to implement algorithms like encryption, compression, and error detection, including control physical devices in the Raspberry Pi project or several other scenarios. Users can also term the Python bitwise operators as binary operators.

## What is XOR in Python?

The Python XOR operator, or the "Exclusive OR," is a bitwise logical operator. It returns 1 when either of the operands is 1 (i.e., one operand is 1, and the other is 0), but not both the operands as 1 or both 0. Again, it returns 0 when both the operands are either 0 or both are 1 (i.e., two operands are either 1 or 0).

Users can define the XOR relation between the operands using the bitwise XOR operator, i.e., the '^' symbol in Python or the '⊕' symbol in Mathematics.

XOR works on two Booleans

XOR works on two Integers

## How to implement the Bitwise Exclusive OR operation in Python operands?

In Python, users can implement the bitwise XOR operation using the '^' symbol. They can use the XOR operation for different purposes.

These are the XOR of two integers, Swapping two numbers using XOR, the XOR of two Booleans, and many more.

## Let us see how XOR works on two Booleans:

 A B A⊕B False False False True False True False True True True True False

## Using ‘^’ XOR operator

Code Snippet:

``````>>> True ^ False
True
>>> False ^ True
True
>>> True ^ True
False
>>> False ^ False
False``````

Output: Explanation:

As discussed above, the XOR "^" operator returns true only when any one operand is true or 1. Else, returns false in the case when both the operands are either true or false.

## Using the XOR Function

Code Snippet:

``````from operator import XOR
XOR(True, False)
XOR(True, True)
XOR(False, False)``````

Output: Explanation:

Here, we used the XOR function from the operator module, and it performs the same bitwise operation as the '^' operator. But the syntax is different from the XOR '^' operator.

Code Snippet:

``````x = True
y = False
m = x ^ x
n = x ^ y
o = y ^ x
p = y ^ y
print(x, "⊕ " , x, "=", m)
print(x, "⊕ " , y, "=", n)
print(y, "⊕ " , x, "=", o)
print(y, "⊕ " , y, "=", p)``````

Output: ## Let us see how XOR works on two Integers:

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

## Using the '^' XOR operator

Code Snippet:

``````>>> 0 ^ 0
0
>>> 1 ^ 0
1
>>> 1 ^ 1
0``````

Output: Explanation:

As discussed above, the XOR "^" operator returns true only when any one operand is 1 (or any one operand is 0, i.e., different operand values). Else, it returns false when both operands are either true or false.

## Using the XOR Function

Code Snippet:

``````from operator import XOR
XOR(1, 0)
XOR(1, 1)
XOR(0, 0)``````

Output: Explanation:

In the above example, we used the XOR function from the operator module, and it performs the same bitwise operation as the '^' operator. But the syntax is different from the XOR '^' operator.

Code Snippet:

``````x = 30
y = 26
z = x ^ y
print (z)``````

Output: Explanation:

We used the XOR operator to implement bitwise operations on the two integers, 30 and 26 in the above code sample. The XOR operator first converts the integer values in the binary form and then compares the bits bitwise.

Let us dig deeper into this example of the XOR in Python with the following example:

``````x = 30
y = 26``````

Now we convert the two integers into binary forms:

``````x = 011110
y = 011010``````

Therefore, 30 26, i.e., 011110 011010 = 000100, i.e., 4

## Using Logical 'and' and 'or' Operators

Users also use the logical 'and' and 'or' operators in addition to the XOR '^' operator or the XOR function.

Code Snippet:

``````>>> (True and not False) or (not True and False)
True
>>> (True and not True) or (not True and True)
False
>>> (False and not False) or (not False and False)
False``````

Output: Explanation:

The above example explains the same bitwise operation as the XOR operator or the XOR function but is not more efficient than the previous one. It involves more operations compared to the Exclusive OR operations.

## Using the functools.reduce Function

It is an alternative approach to implementing Exclusive OR, i.e., by using reduce() function from the functools module.

Code Snippet:

``````>>> from functools import reduce
>>> from operator import XOR
>>> reduce(XOR, [False, True, False, True])
False
>>> reduce(XOR, [False, True, True, True])
True``````

Output: Explanation:

In this example, we used the reduce function with the XOR function. The reduce() uses the Exclusive OR function for each value of the Python list. It starts with the first two Boolean values and then continues with the result of the previous operation. It calculates the resultant value with the next value in the list.

In the first case, the final result is False because the number of true values in the list is even. And in the second case, the final result is True because the number of true values in the list is odd.

Conclusion

We hope you grasp all the various examples and code snippets of the Exclusive OR operator. Performing bitwise operations with XOR is simple and concise in four different ways. These ways are –

• Using XOR '^' operator
• Using XOR function
• Using Logical AND and OR Operators
• Using functools.reduce function

Recommended Posts:

Contact Information

#3940 Sector 23,
Gurgaon, Haryana (India)
Pin :- 122015

contact@stechies.com

Top Tutorials

Top Interview Questions