Shift Operators In this article we will learn about the shift operator in python. There are two type of shift operator in python. Left Shift operator and Right Shift operator. These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. These operator also called bitwise operator. Bitwise Right operator and Betwise left Operator.
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.
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.
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40
0 Comments