+ 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😃

22nd Oct 2020, 9:33 AM
Si Thu Htun
Si Thu Htun - avatar
4 Respostas
+ 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.
22nd Oct 2020, 12:09 PM
Seb TheS
Seb TheS - avatar
+ 2
Those bitwise operators are really slow. Don't recommend using them.
22nd Oct 2020, 9:42 AM
RainStorm
RainStorm - avatar
22nd Oct 2020, 12:21 PM
QTWizard
+ 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)
22nd Oct 2020, 9:47 AM
QTWizard