Skip to content

Instantly share code, notes, and snippets.

@lukasz-madon
Created September 24, 2013 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukasz-madon/6685451 to your computer and use it in GitHub Desktop.
Save lukasz-madon/6685451 to your computer and use it in GitHub Desktop.
Reversing bits in python e.g 13 which is 1101 becomes 1011 which is 11
def reverse_bits(x):
out = 0
len = 0
num = x
while num > 0:
num /= 2
len += 1
for i in xrange(0, len):
bit = x >> i & 1
out |= bit << (len - 1 - i)
return out
print reverse_bits(13)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment