Skip to content

Instantly share code, notes, and snippets.

@otykhonruk
Created February 18, 2019 22:32
Show Gist options
  • Save otykhonruk/3560b7be4dc9caff0512c83bb3a6c29e to your computer and use it in GitHub Desktop.
Save otykhonruk/3560b7be4dc9caff0512c83bb3a6c29e to your computer and use it in GitHub Desktop.
def reverse_bits(n):
assert 1 <= n <= 1000000000,\
'argument must be in range [1, 1000000000]'
r = 0
while n:
r <<= 1
r |= n & 1
n >>= 1
return r
if __name__ == '__main__':
import sys
if len(sys.argv) > 1 and sys.argv[1].isdigit():
print(reverse_bits(int(sys.argv[1])))
else:
print('Usage: {} <N>'.format(sys.argv[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment