# XOR in Python

In Python, you can perform the XOR operation using the ^ operator. The ^ operator takes two integers as inputs and returns an integer representing the result of the bitwise XOR operation.

For example, let's say that we want to perform the XOR operation on the binary numbers 1010 and 1100. In Python, we can represent these binary numbers using the prefix 0b followed by the binary digits:

``````a = 0b1010
b = 0b1100
``````

To perform the XOR operation, we can simply use the ^ operator:

`c = a ^ b`

The value of c will be the result of the XOR operation, which is 0b0110 in binary or 6 in decimal. To print the result in binary or decimal format, you can use the bin() and int() functions, respectively:

``````print(bin(c)) # Output: 0b110
print(int(c)) # Output: 6``````

## How to perform the bitwise XOR in Python?

In Python, you can perform the bitwise XOR operation using the "^" operator.

### Here's an example:

``````a = 0b1010 # binary representation of 10
b = 0b1100 # binary representation of 12
result = a ^ b # perform bitwise XOR operation
print(result) ``````

Output:

`6 (binary representation of 0b0110)`

In this example, the ^ operator is used to perform the bitwise XOR operation on the binary representations of the numbers a and b. The result is stored in the result variable, which is then printed to the console.

Note that you can also perform bitwise XOR on integers represented in decimal or hexadecimal format by simply assigning the value to a variable.

For example:

``````a = 10 # decimal representation of 10
b = 12 # decimal representation of 12
result = a ^ b # perform bitwise XOR operation
print(result) # output: 6 (decimal representation of 6)
``````

In this example, the bitwise XOR operation is performed on the decimal representations of the numbers a and b. The result is also stored in the result variable, which is then printed to the console.

## XOR ^ Operator between 2 integers

The XOR (^) operator between two integers performs a bitwise XOR operation between their binary representations. Each bit in the result is the XOR of the corresponding bits in the input integers.

Here's an example:

``````a = 9 # binary representation: 0b1001
b = 14 # binary representation: 0b1110
result = a ^ b # perform bitwise XOR operation
print(result) # output: 7 (binary representation: 0b0111)``````

In this example, the binary representation of a is 0b1001 and the binary representation of b is 0b1110. Performing a bitwise XOR operation between these two integers gives a result of 0b0111, which is equivalent to decimal 7.

Note that if the binary representations of the two input integers are different in length, Python will automatically zero-pad the shorter representation with leading zeros to match the length of the longer representation before performing the XOR operation.

## Performing XOR on two Booleans

In Python, you can use the ^ operator to perform the XOR operation on two Boolean values.

Here's an example:

``````a = True
b = False
result = a ^ b
print(result) # output: True``````

In this example, the Boolean value True is assigned to the variable a, and the Boolean value False is assigned to the variable b. The ^ operator is used to perform the XOR operation on these two Boolean values, resulting in True.

The XOR operation on Boolean values returns True if the two operands are different, and False if they are the same. In this case, a and b are different, since a is True and b is False, so the result is True.

Note that XOR operations on Boolean values are different from bitwise XOR operations on integers. In bitwise XOR, each bit of the integers is compared and the result is a new integer with bits set according to the XOR operation. On the other hand, the XOR operation on Boolean values simply returns True if the operands are different and False if they are the same.

## XOR in Python using Operator Module

In Python, you can also perform the XOR operation using the operator module. The operator module provides a function called xor() that can be used to perform the XOR operation on two values.

Here's an example:

``````import operator
a = 0b1010 # binary representation of 10
b = 0b1100 # binary representation of 12
result = operator.xor(a, b)
print(result) # output: 6 (binary representation of 0b0110)``````

In this example, we import the operator module and use the xor() function to perform the bitwise XOR operation on the binary representations of a and b. The result is stored in the result variable, which is then printed to the console.

Note that the operator.xor() function works the same way as the ^ operator when used with integers. However, it can also be used with other types of values, such as Booleans.

For example:

``````import operator
a = True
b = False
result = operator.xor(a, b)
print(result) ``````

Output:

`True`

In this example, we use the operator.xor() function to perform the XOR operation on the Boolean values a and b. The result is True, since a and b are different.