Is there a standard library way in python to get the equivalent of a number reading bits in reverse ? (Apart from converting to a string, reversing it, and converting back)
Example of expected behavior :
>>> reverse(20) # 20 = b10100
5 # 5 = b00101
>>> reverse(11) # 11 = b1011
13 # 13 = b1101
Current (slightly improved, still janky) solution :
def reverse(n):
return int(bin(n)[:1:-1], 2)