Python provides many bitwise operators like AND, OR, XOR, etc. In this session, we will learn about the python xor function. so let’s get started.

## What is the xor function in Python?

In Python, a bitwise operator is an operator used to perform a bitwise operation on two integer operands. A bitwise operation operates on the individual bits of an integer, rather than on the integer as a whole.

One such bitwise operator is the python XOR function. You can use the ^ operator to perform bitwise XOR operation. For each bit position in the first operand, the bitwise XOR operation compares the corresponding bit in the second operand. If the bits are different, the result bit in that position is set to 1. If the bits are the same, the result bit is set to 0.

### Syntax of python xor function

Below is the general syntax of the python xor function.

``xor_num = operand1 ^ operand2``

The operand can be an integer, string, list or boolean.

## Example of python xor function

In this session, we will explore different examples of using the Python xor function.

### Python xor between two boolean

In Python, we can use the xor(`^)` operator to perform a bitwise XOR operation on two booleans. Note that in Python, the `True` and `False` values are treated as 1 and 0, respectively, when used in a bitwise operation.

Therefore, the bitwise XOR of `True` and `True` is equivalent to the bitwise XOR of 1 and 1, which is 0 (False). Similarly, the bitwise XOR of `True` and `False` is equivalent to the bitwise XOR of 1 and 0, which is 1 (True).

Let’s understand this with the below example:

```def xor(a, b):
return a ^ b

print(xor(True, True))
print(xor(False, False))
print(xor(True, False))
print(xor(False, True))```

This function takes two booleans as input and returns the bitwise XOR between them. The above code returns the below output

```False
False
True
True```

### Python xor between two integers

A user can also use the xor(^) function to perform a bitwise XOR operation on two integers. Below is an example of how you could implement a function to perform a bitwise XOR operation on two integers:

```def xor(a, b):
return a ^ b

print(xor(1, 2))
print(xor(5, 3))```

The bitwise XOR of 1 (0001 in binary) and 2 (0010 in binary) is 3 (0011 in binary) because only the first and second bits are different.

Similarly, the bitwise XOR of 5 (0101 in binary) and 3 (0011 in binary) is 6 (0110 in binary), because only the first and third bits are different.

### Python xor between two list

The Python xor(^) operator can be used to perform a bitwise XOR operation on the elements of two lists. However, this operation will only work if both lists are of identical length and contain only integers or boolean values.

Below is an example of how you could implement a function to perform a bitwise XOR operation on the elements of two lists in Python:

```def xor_lists(l1, l2):
result = []
for x1, x2 in zip(l1, l2):
result.append(x1 ^ x2)
return result

print(xor_lists([0, 1, 1, 0], [0, 1, 0, 1]))
print(xor_lists([True, True, True, False], [False, True, False, True]))```

Please note that the resulting list will contain the same type of elements as the input lists (either integers or booleans). If the lists are not of the same length, the function will raise a `ValueError` exception

### Python xor between two strings

You can also use the xor(`^)` operator to perform a bitwise XOR operation on two strings. However, this operation will only work if both strings are of the same length, and the strings must contain only ASCII characters.

Below is an example of how you could implement a function to perform a bitwise XOR operation on two strings:

```def xor_strings(s1, s2):
b1 = s1.encode()
b2 = s2.encode()
# XOR the bytes
result = b''
for x1, x2 in zip(b1, b2):
result += bytes([x1 ^ x2])
# Return the XOR'd string
return result.decode()

print(xor_strings('hello', 'world')) ```

The final string may contain non-printable characters, as the XOR operation is performed on the individual bytes of the strings, and not on the characters themselves. Below is the output of the above code snipped.

``'\x1b\x1b\x1d\x1d\x1c'``

### Python xor between two hexadecimal values

We can also use the xor(`^)` operator to perform a bitwise XOR operation on two hexadecimal values. Please refer to the below code snippet.

```def xor_hex(h1, h2):
# Convert the hexadecimal values to integers
i1 = int(h1, 16)
i2 = int(h2, 16)
# XOR the integers
result = i1 ^ i2
# Return the result as a hexadecimal string
return hex(result)

print(xor_hex('0x1', '0x2'))
print(xor_hex('0x7', '0x4'))```

The above function takes two hexadecimal values as input and returns the bitwise XOR of the two values as a hexadecimal string.

The hexadecimal values must first be converted to integers using the int() function and a base of 16. The ^ operator can then be used to perform the bitwise XOR operation on the integers. The result of the operation can be converted back to a hexadecimal string using the hex() function.

The bitwise XOR of `0x1` (1 in decimal) and `0x2` (2 in decimal) is `0x3` (3 in decimal), because only the first bit is different. Similarly, the bitwise XOR of `0x`7 (7 in decimal) and `0x`4 (4 in decimal) is `0x`3 (6 in decimal).

## XOR in Python using Operator Module

In Python, you can use the `operator` module to perform a bitwise XOR operation on two values. To use the `operator` module, you will first need to import it. Below is an example of how you could use the `operator` module to perform a bitwise XOR operation in Python:

```import operator
def xor(a, b):
return operator.xor(a, b)

print(xor(1, 2))
print(xor(7, 4))```

The `xor` function from the `operator` module works with any values that support the bitwise XOR operation, including integers, booleans, and strings. Below is the output for the same.

## Conclusion

I hope you have liked this tutorial on the python xor function. In this tutorial, we have learned the python xor function with multiple examples. Please do let me know if you are facing any issues while following along.

## More to Explore

Python substring

Python remove an element from a list

Python square root

Python NULL detailed tutorial

Python lowercase function

Python array length

Scroll to Top