What does the >> operator do?
For example 10 >> 1 = 5
Thanks
What does the >> operator do?
For example 10 >> 1 = 5
Thanks
It's the right bit shift operator, 'moves' all bits once to the right.
10 in binary is
1010
shifted to the right it turns to
0101
which is 5
See section 5.7 Shifting Operations in the Python Reference Manual.
They shift the first argument to the left or right by the number of bits given by the second argument.
Its the right shift
operator.
10
in binary is 1010
now >> 1
says to right shift by 1
, effectively loosing the least significant bit to give 101
, which is 5
represented in binary.
In effect it divides
the number by 2
.