Reverse Bits
Part of Practice: Counting & Canceling
You are given a 32-bit unsigned integer n. Return the integer whose binary representation is n's 32 bits in reverse order: the lowest bit (bit 0) moves to the highest position (bit 31), bit 1 moves to bit 30, and so on.
All 32 positions count, including leading zeros — reversing 1 does not give 1, it gives a 1 in the top position followed by 31 zeros. The result is also read as an unsigned 32-bit value.
Examples
- Input:
- n = 43261596 (binary 00000010100101000001111010011100)
- Output:
- 964176192 (binary 00111001011110000010100101000000)
Reading the input's 32 bits right-to-left produces the output's bits left-to-right.
- Input:
- n = 1
- Output:
- 2147483648
Bit 0 moves to bit 31: the result is 1 followed by 31 zeros, which is 2^31.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.