Shift numbers in Python mean we need to perform bitwise shift operations on numbers. There are two types of shift possible, bitwise left shift and bitwise right shift.
Notation of bitwise left shift: 8 << 2
Here, 8 will be shifted bitwise towards the left by 2 units/places.
Example of bitwise left shift:
a = 8 = 0000 1000
a<<2 = 0010 0000 = 32
Notation of bitwise right shift: 8 >> 2
Here, 8 will be shifted bitwise towards the right by 2 units/places.
Example of bitwise right shift:
a = 8 = 0000 1000
a>>2 = 0000 0010 = 2
Code to Shift Numbers in Python
# bitwise left shift
print("8 << 2 = ", 8 << 2)
# bitwise right shift
print("8 >> 2 = ", 8 >> 2)
Output:
8 << 2 = 32
8 >> 2 = 2
Also Read:
- Python | Check Armstrong Number using for loop
- Python | Factorial of a number using for loop
- Create your own ChatGPT with Python
- Radha Krishna using Python Turtle
- Python Programming Examples | Fundamental Programs in Python
- Python | Delete object of a class
- Python | Modify properties of objects
- Python classmethod
- Python | Create a class that takes 2 parameters, print both parameters
- Python | Create a class, create an object of the class, access, and print property value
- Python | Find the maximum element in a list using lambda and reduce()
- Python | filter numbers(that are not divisible by 3 and 4) from a list using lambda and filter()
- Python | lambda function that returns the maximum of 2 numbers
- Python | Convert all strings of a list to uppercase using lambda
- Python | Square numbers of a list using lambda
- Python | Reverse and convert a string to uppercase using lambda
- Python | Convert String to uppercase using lambda
- Python | Reverse a list using lambda
- Python | Calculator using lambda
- Python | Square of a number using lambda
- Python | Multiply numbers using lambda
- Python | lambda with None
- Python | lambda with pass statement
- Python | Add numbers using lambda
- Python | Print Namaste using lambda
- Iterate over a string in Python
- Python | join() | Join list of strings
- Python | isalnum() method | check if a string consists only of alphabetical characters or numerical digits
- Python | isupper() and islower() methods to check if a string is in all uppercase or lowercase letters
- Python | count substring in a String | count() method