#74 · Bit manipulation · Intermediate · 65 XP · about 10 min
Treat n as a 32-bit unsigned integer: exactly 32 binary digits, with leading zeros where needed. Reverse the order of those 32 bits, so the last bit becomes the first, and find the number they now make.
Write reverse_bits(n) that returns that reversed value as an ordinary, non-negative integer.
reverse_bits(1) # returns 2147483648 reverse_bits(43261596) # returns 964176192 reverse_bits(0) # returns 0
1 is 31 zeros followed by a 1. Reversed, it is a 1 followed by 31 zeros, which is 2^31 = 2147483648.
Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.