How is xor calculated in python?

How is xor calculated in python?

Python bitwise operators are used to perform bitwise calculations on integers. First, the integers are converted into binary format, and then operations are performed bit by bit, hence the name of the bitwise operators.

Python bitwise operators work on integers only, and the final output is returned in the decimal format. Python bitwise operators are also called binary operators.

XOR in Python is known as “exclusive or”, which compares two binary numbers bitwise. If both bits are the same, the XOR operator outputs 0. If both bits are different, the XOR operator outputs 1.

The Bitwise XOR sets the input bits to 1 if either, but not both, of the analogous bits in the two operands is 1.

Use the XOR operator ^ between two values to perform bitwise “exclusive or” on their binary representations.

For example, when used between two integers, the XOR operator returns an integer.

output = 19 ^ 21

print(output)

Output

6

We have used the XOR operator between two integers. When used between two integers, the XOR operator returns an integer.

When performing XOR on two booleans, True is treated as 1, and False is treated as 0. Thus, XOR between two booleans returns a boolean.

result = True ^ False

print(result)

Output

True

Let’s compare two False values.

result = False ^ False

print(result)

Output

False

Let’s compare two True values.

result = True ^ True

print(result)

Output

False

From the above code example, you can see that if two True or False values are compared, it returns False, but if two different values are compared, it will return True.

More Examples

See the following code.

result = bin(0b1111 ^ 0b1111)

print(result)

Output

0b0

Let’s see how to swap integers without a temporary variable using XOR.

a = 21
b = 19

print('The value of a is: ', a)
print('The value of b is: ', b)

a ^= b
b ^= a
a ^= b

print('After swapping: ')
print('The value of a is: ', a)
print('The value of b is: ', b)

Output

The value of a is:  21
The value of b is:  19
After swapping:
The value of a is:  19
The value of b is:  21

That’s it for this tutorial.

See also

Python And

Python’s bitwise XOR operator x ^ y performs logical XOR on each bit position on the binary representations of integers x and y. Each output bit evaluates to 1 if and only if exactly one of the two input bits at the same position are 1. For example, the integer expression 4 ^ 3 is translated to the binary operation 0100 ^ 0011 which results in 0111 because for the last three positions exactly one bit is 1.

As you go over the article, you can watch my explainer video here:

Python Bitwise XOR ^ Operator

In this example, you apply the bitwise XOR operator to two integers 32 and 16:

>>> 32 ^ 16
48

The expression 32 ^ 16 operates on the bit representations "0100000" (decimal 32) and "0010000" (decimal 16) and performs bitwise XOR resulting in binary "0110000" (decimal 48):

First Operand x 1 0 0 0 0 0
Second Operand y 0 1 0 0 0 0
x ^ y 1 1 0 0 0 0

Similarly, let’s have a look at a slightly modified example to showcase the bitwise XOR operator:

>>> 31 ^ 15
16

You can see this in the following visualization:

First Operand x 1 1 1 1 1
Second Operand y 0 1 1 1 1
x ^ y 1 0 0 0 0

The decimal representation of the bit sequence "10000" is 16.

  • Python Bitwise ^ Operator Example
  • Representing Negative Integers in Binaries
  • Python Bitwise XOR ^ Examples on Negative Integers
  • Python Bitwise XOR List and Set
  • Python Bitwise XOR Bool
  • Python Bitwise XOR Assignments (Equal)
  • Python Bitwise XOR Overloading
  • Bitwise Operators and Magic Methods

Here’s the result of the bitwise XOR operator x ^ y when applied to a couple of example integer operands x and y:

x (int)y (int)x (binary)y (binary)x ^ y (binary)x ^ y (int)
0 1 0 1 1 1
0 3 00 11 11 3
3 5 0011 0101 0110 6
33 129 000100001 010000001 010100000 160
15 7 01111 00111 01000 8
14 7 01110 00111 01001 9

You can see those examples in the following Python script:

>>> 0 ^ 1
1
>>> 0 ^ 3
3
>>> 3 ^ 5
6
>>> 33 ^ 129
160
>>> 15 ^ 7
8
>>> 14 ^ 7
9

Next, you’ll learn how to use the operator on negative integers. But first, you need to understand how negative integers are represented in the first place. This will boost your computer science skills, so keep reading! ?‍?

Representing Negative Integers in Binaries

Python uses so-called complementary binaries to represent negative integers. The first bit of a complementary binary is the sign (0: positive, 1: negative). All remaining bits encode the number. You write a negative number -x as the bit pattern for (x-1) and flip all bits from 1 to 0 and from 0 to 1 (complement).

Here are two simple examples:

  • To represent x = -1 using 8 bits you first calculate (1-1) = 0 and then flip all bits to calculate "11111111".
  • To represent x = -10 using 8 bits you first calculate (10-1) = 9 which is "00001001" in binary format. Then, you complement all bits to determine the negative (complementary) binary "11110110".

Let’s use this knowledge in a couple of examples to showcase the working of the bitwise XOR operator on negative integers:

Python Bitwise XOR ^ Examples on Negative Integers

Here’s the result of the bitwise XOR operator x ^ y when applied to example negative integer operands x and y:

x (int)y (int)x (binary)y (binary)x ^ y (binary)x ^ y (int)
0 -1 00000000 11111111 11111111 -1
0 -3 00000000 11111101 11111101 -3
-3 -5 11111101 11111011 00000110 6

You can see those examples in the following script:

>>> 0 ^ -1
-1
>>> 0 ^ -3
-3
>>> -3 ^ -5
6

Python Bitwise XOR List and Set

To perform a bitwise XOR operation on all elements in a given list of integers, import the NumPy library using import numpy as np and call np.bitwise_xor.reduce(my_list).

Here’s an example:

import numpy as np

my_list = [1, 2, 3, 4, 5]
res = np.bitwise_xor.reduce(my_list)
print(res)
# 1

You can see the computation in the following table—the last row being the result of the bitwise XOR operation on all elements in the list [1, 2, 3, 4, 5] that correspond to binaries 0001, 0010, 0011, 0100, and 0101.

Pos 0Pos 1Pos 2Pos 3
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 0 0 1

To calculate this, you have to perform XOR along each given column, two rows at a time. If they’ve different bits, the intermediate result is 1, otherwise 0. Then, you use the result and compare it with the next row, and so on. The final result is 1.

Alternatively, if you want to perform a bitwise XOR on all elements in a set, you can use the same idea of importng the NumPy library using import numpy as np and calling np.bitwise_xor.reduce(my_set).

import numpy as np

my_set = [1, 2, 3, 4, 5]
res = np.bitwise_xor.reduce(my_set)
print(res)
# 1

Python Bitwise XOR Bool

The Boolean values True and False can be semantically represented by a single bit 1 and 0. Thus, if you apply the bitwise XOR operation on two Boolean values using x ^ y, the result is True if and only if exactly one of the operands is True.

Here are the four ways to apply the bitwise XOR operator to Boolean values:

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

Python Bitwise XOR Assignments (Equal)

The equal symbol after the bitwise OR operator (x ^= y) performs bitwise XOR assignment operation. It calculates bitwise XOR first and assigns the result to the variable x. The bitwise XOR assignment expression x ^= y is syntactic sugar for the semantically identical x = x ^ y. The return value of the operator is None but it updates the first operand with the result of the operation.

Here you can see that the variable x changes after applying the assignment operator:

>>> x = 1
>>> y = 2
>>> x ^= y
>>> x
3
>>> y
2

Here’s the semantically identical version of this without the assignment operator:

>>> x = 1
>>> y = 2
>>> x = x ^ y
>>> x
3
>>> y
2

Python Bitwise XOR Overloading

You can define your own bitwise XOR operator on a custom class by overloading the __xor__ dunder method that allows the expression x | y on your custom objects.

Here’s an example:

class Data:
    def __init__(self, data):
        self.data = data

    def __xor__(self, other):
        return Data(self.data ^ other.data)


x = Data(3)
y = Data(4)

res = x ^ y
print(res.data)
# 7

Note: if you forget to overwrite the __xor__ method and still try to use the expression x ^ y, Python will raise a TypeError: unsupported operand type(s) for ^.

class Data:
    def __init__(self, data):
        self.data = data


x = Data(3)
y = Data(4)

res = x ^ y
print(res.data)

Output:

Traceback (most recent call last):
   File "C:\Users\xcent\Desktop\code.py", line 11, in 
     res = x ^ y
 TypeError: unsupported operand type(s) for ^: 'Data' and 'Data'

To fix this TypeError, simply define the __xor__ method as shown in the working example before.

Bitwise Operators and Magic Methods

Bitwise operators perform operations on the binary (bit) representation of integers. The following table gives a short overview of all existing bitwise operators. Note that we also provide the binary representation 100 for the decimal integer 4, and 101 for the decimal integer 5 as a comment in the right column.

OperatorNameDescription Example
x = 4, y = 5
& Bitwise AND Performs logical AND on a bit-by-bit basis x & y
# b100 & b101 == b100 == 4
| Bitwise OR Performs logical OR operation on a bit-by-bit basis x | y
# b100 | b101 == b101 == 5
~ Bitwise NOT Performs logical NOT on a bit-by-bit basis, inverting each bit so that 0 becomes 1 and 1 becomes 0. Same as -x-1. ~x
# -4-1 == -5
^ Bitwise XOR Performs logical “exclusive or” operation on a bit-by-bit basis x ^ y
# b100 ^ b101 == b001 == 1
>> Bitwise right shift Shifts binary of left operand to the right by the number of positions specified in right operand x >> 2
# b100 == b010 == b001 == 1
<< Bitwise left shift Shifts binary of left operand to the left by the number of positions specified in right operand x << 2
# b100 == b1000 == b10000 == 16

Here’s a short overview of the Bitwise operators’ magic methods:

Bitwise OperatorMagic “Dunder” Method
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)
~ __invert__(self)
<< __lshift__(self, other)
>> __rshift__(self, other)

Here’s an example of how to accomplish these bitwise operators on a custom class Data. We marked this respective operator in the code:

class Data:

    def __init__(self, data):
        self.data = data

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)

The output is:

Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0

How is xor calculated in python?

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

How does Python calculate XOR value?

1- Initialize the result as 0. 1- Traverse all numbers from 1 to n..
Find the remainder of n by moduling it with 4..
If rem = 0, then XOR will be same as n..
If rem = 1, then XOR will be 1..
If rem = 2, then XOR will be n+1..
If rem = 3 ,then XOR will be 0..

How is XOR value calculated?

To find the XOR of two numbers, follow these instructions:.
Convert the numbers into the binary representation..
Compare the corresponding bits of the two numbers..
If only one of the input bits is true (1), the output is true (1). Otherwise, the output is false (0)..

How do you find the XOR of two numbers in Python?

To get the logical xor of two or more variables in Python: Convert inputs to booleans. Use the bitwise xor operator ( ^ or operator. xor ).
That's 100 ns of my life I won't get back ;-) ... .
For an in-between timing, you can do from operator import truth at the top of the module, and test truth(a) !=.

How do you find the XOR of a list in Python?

Method #1 : Using reduce() + lambda + “^” operator We can employ reduce() to accumulate the result of XOR logic specified by the lambda function.