Python Bitwise Left-Shift << Operator

Rate this post

The Python bitwise left-shift operator x << n shifts the binary representation of integer x by n positions to the left. For a positive integer, it inserts a 0 bit on the right and shifts all remaining bits by one position to the left. For example, if you left-shift the binary representation 0101 by one position, you’d obtain 01010. Semantically, the bitwise left-shift operator x << n is the same as multiplying the integer x with 2**n.

Python Bitwise Left-Shift << Operator

Here’s a minimal example:

print(8 << 1)
# 16

print(8 << 2)
# 32

print(-3 << 1)
# -6

Let’s dive deeper into the details next!

Video Explainer

As you go over the article, you can watch my explainer video here:

Example

In this example, you apply the bitwise left-shift operator to integer 32 shifting it by one position:

x = 32

# Shift by one position to the left
res = x << 1
print(res)
# 64

# Shift by two positions to the left
res = x << 2
print(res)
# 128

The bit representation of decimal 32 is "0100000". If you shift it by one position to the left, you obtain binary "01000000" (decimal 64). If you shift by two positions to the right, you obtain binary "010000000" (decimal 128). Here’s the tabular explanation:

x0100000
x << 101000000
x << 2010000000

Each row represents the resulting shifted binary representation of the original integer 32.

Python Bitwise Left-Shift Operator Overloading

To enable the left-shift operator on your custom object, use Python’s operator overloading functionality. Overloading works through what is called magic methods or dunder methods (for “double-underscore methods”). For the left-shift operator, the magic method is the __lshift__(self, other) method. It should return a new custom object that is the result of the bitwise operation.

Here’s a short overview of the Bitwise operators’ magic methods:

Bitwise OperatorMagic “Dunder” Method
&__and__(self, other)
|__or__(self, other)
^__xor__(self, other)
~__invert__(self)
<<__lshift__(self, other)
>>__rshift__(self, other)

Here’s an example of how to accomplish these bitwise operators on a custom class Data. We marked this respective operator in the code:

class Data:

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

    def __and__(self, other):
        return Data(self.data & other.data)

    def __or__(self, other):
        return Data(self.data | other.data)
    
    def __xor__(self, other):
        return Data(self.data ^ other.data)
    
    def __invert__(self):
        return Data(~self.data)
    
    def __lshift__(self, other):
        return Data(self.data << other.data)
    
    def __rshift__(self, other):
        return Data(self.data >> other.data)


x = 2
y = 3
print('Operands: \n', 'x =', x, '\n', 'y =', y)
print()
print('Bitwise AND: ', x & y)
print('Bitwise OR: ', x | y)
print('Bitwise XOR: ', x ^ y)
print('Bitwise NOT: ', ~x)
print('Bitwise LEFT-SHIFT: ', x << y)
print('Bitwise RIGHT-SHIFT: ', x >> y)

The output is:

Operands: 
  x = 2 
  y = 3

 Bitwise AND:  2
 Bitwise OR:  3
 Bitwise XOR:  1
 Bitwise NOT:  -3
 Bitwise LEFT-SHIFT:  16
 Bitwise RIGHT-SHIFT:  0

Bitwise Operators

Bitwise operators perform operations on the binary (bit) representation of integers. The following table gives a short overview of all existing bitwise operators. Note that we also provide the binary representation 100 for the decimal integer 4, and 101 for the decimal integer 5 as a comment in the right column.

OperatorNameDescriptionExample
x = 4, y = 5
&Bitwise ANDPerforms logical AND on a bit-by-bit basisx & y
# b100 & b101 == b100 == 4
|Bitwise ORPerforms logical OR operation on a bit-by-bit basisx | y
# b100 | b101 == b101 == 5
~Bitwise NOTPerforms logical NOT on a bit-by-bit basis, inverting each bit so that 0 becomes 1 and 1 becomes 0. Same as -x-1.~x
# -4-1 == -5
^Bitwise XORPerforms logical “exclusive or” operation on a bit-by-bit basisx ^ y
# b100 ^ b101 == b001 == 1
>>Bitwise right shiftShifts binary of left operand to the right by the number of positions specified in right operandx >> 2
# b100 == b010 == b001 == 1
<<Bitwise left shiftShifts binary of left operand to the left by the number of positions specified in right operandx << 2
# b100 == b1000 == b10000 == 16