Search code examples
pythonbinarybit-shift

Usage of bitwise shifting


I can understand quite well how bit shifting works, but I can't find any situation where I can use it for faster/better result. Could someone explain when it's useful to use operators >> and <<?

x = 2
y = 7
z = 16

a = x << y
print(bin(x), "with %d zeros removed from left/added on right ->" % y, bin(a))

b = z >> x
print(bin(z), "with %d zeros removed from right/added on left ->" % x, bin(b))

Solution

  • Not really sure where it will be useful in usual coding. But when it comes to embedded programming this has immense usage. To manipulate registers I used to use bit-wise operators quite often.