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

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 AND operator first—because if you’ve mastered bitwise AND, understanding the in-place bitwise AND operator is a breeze in the summer.

Table of Contents

## Bitwise AND Refresher

Python’s ** bitwise AND** operator

`x & y`

performs *logical AND*on each bit position on the binary representations of integers

`x`

and `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

The expression `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 `x` | 1 | 0 | 0 | 0 | 0 | 0 |

Second Operand `y` | 0 | 1 | 0 | 0 | 0 | 0 |

`x & y` | 0 | 0 | 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 `x` | 1 | 1 | 1 | 1 | 1 |

Second Operand `y` | 0 | 1 | 1 | 1 | 1 |

`x & y` | 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 `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 __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 Assignment | `x = 3` | |

`+=` | In-place Addition | `x += 3` | `x = x + 3` |

`-=` | In-place Subtraction | `x -= 3` | `x = x - 3` |

`*=` | In-place Multiplication | `x *= 3` | `x = x * 3` |

`/=` | In-place Division | `x /= 3` | `x = x / 3` |

`%=` | In-place Modulo | `x %= 3` | `x = x % 3` |

`//=` | In-place Integer Division | `x //= 3` | `x = x // 3` |

`**=` | In-place Power | `x **= 3` | `x = x ** 3` |

`&=` | In-place Bitwise And | `x &= 3` | `x = x & 3` |

`|=` | In-place Bitwise Or | `x |= 3` | `x = x | 3` |

`^=` | In-place Bitwise XOR | `x ^= 3` | `x = x ^ 3` |

`>>=` | In-place Bitwise Shift Right | `x >>= 3` | `x = x >> 3` |

<<= | In-place Bitwise Shift Left | `x <<= 5` | `x = x << 5` |

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.