Python’s in-place bitwise XOR operator
x ^= y calcualtes bitwise XOR
x 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
x is syntactical sugar for the longer-form
x = x :
>>> 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
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
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 ||1||0||0||0||0||0|
|Second Operand ||0||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 ||1||1||1||1||1|
|Second Operand ||0||1||1||1||1|
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
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
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.
|Operator||Name||Short Example||Equivalent Long Example|
|In-place Integer Division|
|In-place Bitwise And|
|In-place Bitwise Or|
|In-place Bitwise XOR|
|In-place Bitwise Shift Right|
|<<=||In-place Bitwise Shift Left|
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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.