Python In-Place Bitwise XOR Operator

Python’s in-place bitwise XOR operator x ^= y calcualtes bitwise XOR x ^ y and assigns the result to the first operand x. To set this up for your own class, override the magic “dunder” method __ixor__(self, other) in your class definition.

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

The expression x ^= y is syntactical sugar for the longer-form x = x ^ y:

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

Let’s dive into the bitwise XOR operator first—because if you’ve mastered bitwise XOR, understanding the in-place bitwise XOR operator is a breeze in the summer.

Bitwise XOR Refresher

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:

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 x100000
Second Operand y010000
x ^ y110000

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 x11111
Second Operand y01111
x ^ y10000

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

Related Article: Python Bitwise XOR Operator Deep Dive

Python In-Place Bitwise XOR Magic Method

To use the in-place bitwise XOR operator ^= on custom objects, define the __ixor__() method (“dunder method”, “magic method”) that takes two arguments self and other, updates the first argument self with the result of the operation, and returns the updated object.

In the following code, you calculate the result on two Data objects:

class Data:

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

    def __ixor__(self, other):
        self.data ^= other.data
        return self

x = Data(1) # b001
y = Data(3) # b011

x ^= y # b001
print(x.data)
# 2

You can see that the content of the first operand is updated as a result of the in-place bitwise XOR operation.

Python In-Place Operators

In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, x += 3 is the same as x = x + 3 of first calculating the result of x +3 and then assigning it to the variable x.

OperatorNameShort ExampleEquivalent Long Example
=In-place Assignmentx = 3
+=In-place Additionx += 3x = x + 3
-=In-place Subtractionx -= 3x = x - 3
*=In-place Multiplicationx *= 3x = x * 3
/=In-place Divisionx /= 3x = x / 3
%=In-place Modulox %= 3x = x % 3
//=In-place Integer Divisionx //= 3x = x // 3
**=In-place Powerx **= 3x = x ** 3
&=In-place Bitwise Andx &= 3x = x & 3
|=In-place Bitwise Orx |= 3x = x | 3
^=In-place Bitwise XORx ^= 3x = x ^ 3
>>=In-place Bitwise Shift Rightx >>= 3x = x >> 3
<<=In-place Bitwise Shift Leftx <<= 5x = x << 5