Bit Manipulation - Reverse Bits
The following code attempts to reverse bits of a 32-bit unsigned integer using divide and conquer masks:
def reverseBits(n: int) -> int:
n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1)
n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2)
n = ((n >> 4) & 0x0f0f0f0f) | ((n & 0x0f0f0f0f) << 4)
n = ((n >> 8) & 0x00ff00ff) | ((n & 0x00ff00ff) << 8)
n = (n >> 16) | (n << 16) # Bug here: missing mask
return n
What is the bug in this code?