easyBit ManipulationPure DSA~12 min
Endian Flag Reverse
A 32-bit capability flag word must be transmitted to a peer that reads bits in the opposite order. Produce the bit-reversed word.
Problem
Given an unsigned 32-bit integer n, return the integer obtained by reversing the order of its 32 bits (bit 0 swaps with bit 31, bit 1 with bit 30, and so on).
Input
An unsigned 32-bit integer n (0 ≤ n < 2^32).
Output
An unsigned 32-bit integer — the bit-reversed value.
Constraints
- Exactly 32 bits are reversed
- Aim for O(1) time over the fixed 32-bit width
Examples
Example 1
Input
n = 0b00000010100101000001111010011100
Output
0b00111001011110000010100101000000
Each bit is moved to the mirrored position.
Example 2
Input
n = 0b11111111111111111111111111111101
Output
0b10111111111111111111111111111111
Only the single 0 bit moves to the opposite end.