Shift Operators


This program is a simple Python script that demonstrates the use of shift operators in Python.

Here's a step-by-step explanation of what the code does:

  • The first line prompts the user to input a number, which is then converted to an integer using the int() function.
  • The next line prints the message "Shift Operator Example" to inform the user that the following outputs demonstrate the use of shift operators.
  • The next two lines demonstrate the right shift and left shift operators, respectively.
  • The right shift operator (>>) shifts the bits of the number to the right by the specified number of positions.
  • The left shift operator (<<) shifts the bits of the number to the left by the specified number of positions.
  • The results of the shift operations are then printed for the user to see.

Note that in this code, the shift operator is being used to shift the bits of the number to the right or left by 1 position, but it can be used to shift by any number of positions.

Source Code

number  =int(input("Enter Number : "))
print("Shift Operator Example ")
print("Right Shift ",number,">>1"," Result = ",number>>1)
print("Left Shift ",number,"<<1"," Result = ",number<<1)
To download raw file Click Here

Output

Enter Number : 5
Shift Operator Example
Right Shift  5 >>1  Result =  2
Left Shift  5 <<1  Result =  10