Reverse Bits
Easy · Bit Manipulation · Bit Manipulation · Divide and Conquer
Problem
Reverse the bits of a 32-bit unsigned integer and return the result.
The width is fixed at 32, so leading zeros participate in the reversal.
Examples
Input: n = 43261596 Output: 964176192
00000010100101000001111010011100 reversed is 00111001011110000010100101000000.
Input: n = 1 Output: 2147483648
The lowest bit moves to the highest position.
Constraints
- The input is treated as a 32-bit unsigned integer
Approaches
- Swap in halves — time O(log 32), space O(1)
Follow-up
If this function is called many times, how would you optimise it?
Related problems
Start free on Cariara · Coding problems · System design studio