Given a decimal integer (eg. 65), how does one reverse the underlying bits in Python? i.e.. the following operation:
65 → 01000001 → 10000010 → 130
It seems that this task can be broken down into three steps:
- Convert the decimal integer to binary representation
- Reverse the bits
- Convert back to decimal
Steps #2 and 3 seem pretty straightforward (see this and this SO question related to step #2), but I’m stuck on step #1. The issue with step #1 is retrieving the full decimal representation with filling zeros (ie. 65 = 01000001, not 1000001).
I’ve searched around, but I can’t seem to find anything.
You can specify any filling length in place of the 8. If you want to get really fancy,
lets you specify the width programmatically.