Reverse the 32-bit binary representation of a non-negative integer and return the result as a Long. Treat the input as an unsigned 32-bit value, swap bit i with bit (31 - i) for every i, and return the resulting Long. The return type is Long because the reversed value can exceed the Integer max when the low bit of the original was set. Return null when the input is null or negative.
Examples:
Input: n = 0
Output: 0
Explanation: All bits are zero so the reverse is zero.
Input: n = 1
Output: 2147483648
Explanation: Bit 0 moves to position 31, giving 2 to the 31st power.
Input: n = 5
Output: 2684354560
Explanation: 5 is 101 in binary; reversed in 32 bits gives 2 to the 31st plus 2 to the 29th.
Input: n = 2147483647
Output: 4294967294
Explanation: All low 31 bits set reverses to all high 31 bits set, which equals 2 to the 32nd minus 2.
Input: n = null
Output: null
Explanation: Null input returns null.
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.