What is happening here?
x = 5
x >>= 3
print(x)
the output is 0 and I can't understand why.
What is happening here?
x = 5
x >>= 3
print(x)
the output is 0 and I can't understand why.
It's a shorthand for x = x >> 3. Since 5 >> 3 is 0 you get 0 as a result.
The operator >> is a right shift:
>>> bin(5)
'0b101'
>>> bin(5 >> 1)
'0b10'
>>> bin(5 >> 2)
'0b1'
>>> bin(5 >> 3)
'0b0'
This is basically a right shift operator. Bit Pattern of the data can be shifted by specified number of Positions to Right
5 in bits = 101
101 >> 3. It will return zero.