Python shifting

There are various types of operators like arithmetic operators, comparison operators, and bitwise operators in Python. In our programs, we use these operators to control the sequence of execution and to manipulate the data. In this article, we will study different python bitwise shift operators, their functioning and examples.

Table of Contents

What are Bitwise shift operators?

Bitwise shift operators are binary operators. These operators are used to shift bits of a binary representation of a number to left or right by certain places. Bitwise shift operators are often used for operations in which we have to multiply or divide an integer by powers of 2. Here, the Bitwise left shift operator is used for multiplying a number by powers of 2 while the bitwise right shift operator in python is used to divide a number by powers of 2.  

Bitwise Right Shift Operator in Python

The bitwise right shift operator in python shifts the bits of the binary representation of the input number to the right side by a specified number of places. The empty bits created by shifting the bits are filled by 0s.  

Python shifting

The syntax for the bitwise right shift is a >> n. Here ‘a’ is the number whose bits will be shifted by ‘n’ places to the right.

The working of bitwise right shift operation can be understood from the following illustration.

Suppose we have to shift the bits of 14 by 2 places. We will first convert it into binary format.

  • 14 in binary format is written as 1110.

After shifting, the two rightmost bits 1 and 0 will be discarded and the empty leftmost bits will be filled with 0s. The output of 14 >> 2 will be 0011 in binary which converts to the value 3 in integer format. 

Here you can observe that we have shifted the bits by 2 places due to which the input number has been divided by 22 i.e. 4. Similarly, if we right shift the number by n bits, the integer value of the number will be divided by 2n.  We can verify this output using the right shift operator in python using the following program.

myNum1 = 14
myNum2 = 2
shiftNum = myNum1 >> myNum2
print("Operand 1 is:", myNum1)
print("operand 2 is:", myNum2)
print("Result of the right shift operation on {} by {} bits is {}.".format(myNum1, myNum2, shiftNum))

Output:

Operand 1 is: 14
operand 2 is: 2
Result of the right shift operation on 14 by 2 bits is 3.

Bitwise Left Shift Operator in Python

The bitwise left shift operator in Python shifts the bits of the binary representation of the input number to the left side by a specified number of places. The empty bits created by shifting the bits are filled by 0s.  

The syntax for the bitwise left shift is a << n. Here ‘a’ is the number whose bits will be shifted by ‘n’ places to the left.

The working of bitwise left shift operation can be understood from the following illustration.

Suppose we have to shift the bits of 14 by 2 places. We will first convert it into binary format.

  • 14 in binary format is written as 1110.

After shifting, the empty rightmost bits will be filled with 0s. The output of 14 << 2 will be 111000 in binary which converts to the value 56 in integer format. 

Here you can observe that we have shifted the bits by 2 places due to which the input number has been multiplied by 22 i.e. 4. Similarly, if we left shift the number by n bits, the integer value of the number will be multiplied by 2n.  We can verify this output using the left shift operator in python using the following program.

myNum1 = 14
myNum2 = 2
shiftNum = myNum1 << myNum2
print("Operand 1 is:", myNum1)
print("operand 2 is:", myNum2)
print("Result of the left shift operation on {} by {} bits is {}.".format(myNum1, myNum2, shiftNum))

Output:

Operand 1 is: 14
operand 2 is: 2
Result of the left shift operation on 14 by 2 bits is 56.

Conclusion

In this article, we have discussed bitwise shift operators, their syntax and examples in Python. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.

Related

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic and logical computations. The value the operator operates on is known as Operand. 

Table of Content: 

  •  
  •  

Bitwise operators

In Python, bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format.

Note: Python bitwise operators work only on integers.

OPERATORDESCRIPTIONSYNTAX&Bitwise ANDx & y|Bitwise ORx | y~Bitwise NOT~x^Bitwise XORx ^ y>>Bitwise right shiftx>><<Bitwise left shiftx<<

Let’s understand each operator one by one.

Bitwise AND operator Returns 1 if both the bits are 1 else 0.

Example: 

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a & b = 1010
         &
        0100
      = 0000
      = 0 (Decimal)

Bitwise or operator Returns 1 if either of the bit is 1 else 0.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)

Bitwise not operator: Returns one’s complement of the number.

Example:

a = 10 = 1010 (Binary)

In computers we ususally represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]

~a is basically 1's complement of a 
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result

Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.

2's complement of intermediate-result is:
intermediate-res =  0101      //....1111 0101
      
                     1010      //....0000 1010 -(1's complement)
                         +1    
                 -----------
                   =  1011      //....0000 1011
                  -----------
                   =   -11 (Decimal)
                   
thus ~a = -11

Bitwise xor operator: Returns 1 if one of the bits is 1 and the other is 0 else returns false.

Example:

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a ^ b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)

Python3




# Python program to show

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
0

 

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
1
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
2
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
3

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
4
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
2
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
6

 

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
7

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
a = 10 = 1010 (Binary)

In computers we ususally represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]

~a is basically 1's complement of a 
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result

Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.

2's complement of intermediate-result is:
intermediate-res =  0101      //....1111 0101
      
                     1010      //....0000 1010 -(1's complement)
                         +1    
                 -----------
                   =  1011      //....0000 1011
                  -----------
                   =   -11 (Decimal)
                   
thus ~a = -11
0
a = 10 = 1010 (Binary)

In computers we ususally represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]

~a is basically 1's complement of a 
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result

Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.

2's complement of intermediate-result is:
intermediate-res =  0101      //....1111 0101
      
                     1010      //....0000 1010 -(1's complement)
                         +1    
                 -----------
                   =  1011      //....0000 1011
                  -----------
                   =   -11 (Decimal)
                   
thus ~a = -11
1

 

a = 10 = 1010 (Binary)

In computers we ususally represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]

~a is basically 1's complement of a 
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result

Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.

2's complement of intermediate-result is:
intermediate-res =  0101      //....1111 0101
      
                     1010      //....0000 1010 -(1's complement)
                         +1    
                 -----------
                   =  1011      //....0000 1011
                  -----------
                   =   -11 (Decimal)
                   
thus ~a = -11
2

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
a = 10 = 1010 (Binary)

In computers we ususally represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]

~a is basically 1's complement of a 
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result

Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.

2's complement of intermediate-result is:
intermediate-res =  0101      //....1111 0101
      
                     1010      //....0000 1010 -(1's complement)
                         +1    
                 -----------
                   =  1011      //....0000 1011
                  -----------
                   =   -11 (Decimal)
                   
thus ~a = -11
5
a = 10 = 1010 (Binary)

In computers we ususally represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]

~a is basically 1's complement of a 
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result

Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.

2's complement of intermediate-result is:
intermediate-res =  0101      //....1111 0101
      
                     1010      //....0000 1010 -(1's complement)
                         +1    
                 -----------
                   =  1011      //....0000 1011
                  -----------
                   =   -11 (Decimal)
                   
thus ~a = -11
6

 

a = 10 = 1010 (Binary)

In computers we ususally represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]

~a is basically 1's complement of a 
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result

Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.

2's complement of intermediate-result is:
intermediate-res =  0101      //....1111 0101
      
                     1010      //....0000 1010 -(1's complement)
                         +1    
                 -----------
                   =  1011      //....0000 1011
                  -----------
                   =   -11 (Decimal)
                   
thus ~a = -11
7

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a ^ b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)
0
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a ^ b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)
1

 

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a ^ b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)
2

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a ^ b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)
5
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a ^ b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)
6

Output: 

a & b = 0
a | b = 14
~a = -11
a ^ b = 14

Shift Operators

These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. They can be used when we have to multiply or divide a number by two. 
Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids left( fills 1 in the case of a negative number) as a result. Similar effect as of dividing the number with some power of two.
Example: 

Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 

Bitwise left shift: Shifts the bits of the number to the left and fills 0 on voids right as a result. Similar effect as of multiplying the number with some power of two.
Example: 

Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20 

Example 2:
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40 

Python3




# Python program to show

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a ^ b = 1010
         ^
        0100
      = 1110
      = 14 (Decimal)
8

 

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
1
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
2
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
3

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
4
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
2
a & b = 0
a | b = 14
~a = -11
a ^ b = 14
4
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
3

 

a & b = 0
a | b = 14
~a = -11
a ^ b = 14
6

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
a & b = 0
a | b = 14
~a = -11
a ^ b = 14
9
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
0
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
1
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
2

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
5
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
6
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
1
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
2

 

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
1
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
2
Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20 

Example 2:
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40 
1

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
4
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
2
a & b = 0
a | b = 14
~a = -11
a ^ b = 14
4
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
3

 

Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20 

Example 2:
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40 
6

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20 

Example 2:
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40 
9
a >> 1 = 5
b >> 1 = -5
a << 1 = 10
b << 1 = -20
0
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
1
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
2

a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
8
a = 10 = 1010 (Binary)
b = 4 =  0100 (Binary)

a | b = 1010
         |
        0100
      = 1110
      = 14 (Decimal)
9
a >> 1 = 5
b >> 1 = -5
a << 1 = 10
b << 1 = -20
5
a >> 1 = 5
b >> 1 = -5
a << 1 = 10
b << 1 = -20
6
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
1
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5

Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5 
2

Output: 

a >> 1 = 5
b >> 1 = -5
a << 1 = 10
b << 1 = -20

Bitwise Operator Overloading

Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because the ‘+’ operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading.

How to do shifting in Python?

Bitwise Right Shift Operator Python right shift operator is exactly the opposite of the left shift operator. Then left side operand bits are moved towards the right side for the given number of times. In simple terms, the right side bits are removed.

How do you left shift 1 in Python?

Example. Let's take a number 14. Then 14 << 1 will shift the binary sequence 1 position to the left side.

What is a shift operator in Python?

Bitwise Right shift operator >> is used to shift the binary sequence to right side by specified position.

What is >> and

They are bit shift operator which exists in many mainstream programming languages, << is the left shift and >> is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.