• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Bitwise Shift Operators in Python

Bitwise Shift Operators in Python

Author: Aditya Raj
Last Updated: September 29, 2021

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
  1. What are Bitwise shift operators?
  2. Bitwise Right Shift Operator in Python
  3. Bitwise Left Shift Operator in Python
  4. Conclusion

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.  

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

Recommended Python Training

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us