Created
September 24, 2013 14:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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