+ 2
What does >> do in python?
x = 5 print(x>>1) The output is 2. I don't know why.Please let me know. Thank you😃
4 Réponses
+ 3
RainStorm For simple operations like 5*4 using shift operator may be slow, but when the operands grow, it can still be faster. For example 2<<100 is already much faster than 2*2**100.
But actually I thought bitwise operators were absolutely faster than arithmetic operators, but it might be about Python that they always aren't.
+ 2
Those bitwise operators are really slow. Don't recommend using them.
+ 2
Seb TheS yeah, you are right.
https://code.sololearn.com/cd0MA87cJj9L/?ref=app
+ 1
Shift right operation is equivalent to integer division by 2**nb_bits. You could achieve the same result using:
1- floor division: 5 // 2
2- Int(5/2)