Python’s in-place bitwise XOR operator `x ^= y`

calcualtes bitwise XOR `x `

and assigns the result to the first operand `^`

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

is syntactical sugar for the longer-form `^`

= y`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 `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.

**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.

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.