Python’s in-place bitwise AND operator
x &= y calcualtes bitwise-and
x & y and assigns the result to the first operand
x. To set it up for your own class, override the magic “dunder” method
__iand__(self, other) in your class definition.
>>> x = 1 >>> x &= 2 >>> x 0
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 AND operator first—because if you’ve mastered bitwise AND, understanding the in-place bitwise AND operator is a breeze in the summer.
Bitwise AND Refresher
Python’s bitwise AND operator
x & y performs logical AND on each bit position on the binary representations of integers
y. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0. For example, the integer expression 4 & 3 is translated to binaries 0100 & 0011 which results in 0000 because all four input bit positions are different.
In this example, you apply the bitwise AND operator to two integers 32 and 16:
>>> 32 & 16 0
32 & 16 operates on the bit representations
"010000" (decimal 32) and
"001000" (decimal 16) and performs bitwise AND. As all i-th bit positions are different, the result is 0:
|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 AND operator:
>>> 31 & 15 15
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
"01111" is 15.
Related Article: Python Bitwise AND Operator Deep Dive
Python In-Place Bitwise AND Magic Method
To use the in-place bitwise AND operator
&= on custom objects, define the
__iand__() 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 __iand__(self, other): self.data &= other.data return self x = Data(1) # b001 y = Data(3) # b011 x &= y # b001 print(x.data) # 1
You can see that the content of the first operand is updated as a result of the in-place bitwise AND 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.