Is there a fast possibility to reverse a binary number in python?
Example: I have the number 11 in binary 0000000000001011 with 16 Bits. Now I'm searching for a fast function f, which returns 1101000000000000 (decimal 53248). Lookup tables are no solutions since i want it to scale to 32Bit numbers. Thank you for your effort.
Edit:
Performances. I tested the code for all 2^16 pattern several times.
winner are the partially look up tables: 30ms
2nd
int(format(num, '016b')[::-1], 2)from the comments: 56ms3rd
x = ((x & 0x00FF) << 8) | (x >> 8): 65msI did not expect my approach to be so horribly slow but it is. approx. 320ms. Small improvement by using + instead of | 300ms
bytes(str(num).encode('utf-8'))fought for the 2nd place but somehow the code did not provide valid answers. Most likely because I made a mistake by transforming them into an integer again.
thank you very much for your input. I was quite surprised.