Python In-Place Bitwise Right-Shift Operator

Python’s in-place bitwise right-shift operator x >>= y calculates the right-shift operation x >> y, and assigns the result to the first operands variable name x. You can set up the in-place right-shift behavior in your own class by overriding the magic “dunder” method __irshift__(self, other) in your class definition.

>>> x = 8
>>> x >>= 2
>>> x
2

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

>>> x = 8
>>> x = x >> 2
>>> x
2

Let’s explore some examples on different data types of the operands.

Recap Bitwise Right-Shift

The Python bitwise right-shift operator x >> n shifts the binary representation of integer x by n positions to the right. It inserts a 0 bit on the left and removes the right-most bit. For example, if you right-shift the binary representation 0101 by one position, you’d obtain 0010. Semantically, the bitwise right-shift operator is the same as performing integer division by 2**n.

Python Bitwise Right-Shift >> Operator

Here’s a minimal example:

print(8 >> 1)
# 4

print(8 >> 2)
# 2

print(-3 >> 1)
# -2

Here’s a short explainer video too:

Related. To learn more about the bitwise right-shift operator, and how it works on positive and negative integers, check out our related tutorial: > Operator”>Python Bitwise Right-Shift Operator

Incompatible Data Type

What if two operands have an incompatible data type—unlike floats and integers? For example, if you try to shift a float variable by a list variable (which doesn’t make sense)?

>>> x = 3.0
>>> y = [1, 2]
>>> x >> y
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    x >> y
TypeError: unsupported operand type(s) for >>: 'float' and 'list'

The result of incompatible addition is a TypeError. You can fix it by using only compatible data types for the in-place bitwise right-shift operation.

Can you use the bitwise right-shift operator on custom objects? Yes!

Python In-Place Bitwise Right-Shift Magic Method

To use the in-place bitwise right-shift operator >>= on custom objects, you need to define the __irshift__() 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 combine two Data objects using the in-place right-shift operation:

class Data:

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

    def __irshift__(self, other):
        self.data >>= other.data
        return self

x = Data(8)
y = Data(2)

x >>= y
print(x.data)
# 2

You can see that the content of the first operand is updated as a result of the in-place bitwise right-shift 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