dsa · easy
Reverse Bits
Treat n as an unsigned 32-bit integer. Reverse those 32 bits, including leading zeros, and return the resulting unsigned value.
Arguments
n— unsigned 32-bit integer whose bits you reverse
Example
n = 43261596 is 00000010100101000001111010011100 in 32 bits. Reversing the bit string yields 00111001011110000010100101000000, which is 964176192.
n = 1 is 31 zeros then a one. After the reverse, the one sits in the top bit → 2147483648.
Constraints
0 <= n <= 4294967295
Examples
Example 1
Input: 43261596 Expected: 964176192
Example 2
Input: 1 Expected: 2147483648
Example 3
Input: 0 Expected: 0