Reverse Bits
PROBLEM
Reverse bits of a given 32 bits unsigned integer.
SOLVING
We’ll use Bit Manipulation method
Steps
- Create a uint32_t
result
we’ll return as the answer - Repeat
32
time or the longer of your bitresult
equal toresult
or (|
or bit operation) on the result ofn
and (&
and bit operation) 1 to get the first bit
Example
Example with 6bit (32 is tooooo long) 011101
=n= [011101] & 1 == 1
=result= [000000] << 1
=n= [001110] & 1 == 0
=result= [000001] << 0
=n= [000111] & 1 == 1
=result= [000010] << 1
=n= [000011] & 1 == 1
=result= [000101] << 1
=n= [000001] & 1 == 1
=result= [001011] << 1
=n= [000000] & 1 == 0
=result= [010111] << 0
=result= [101110]
Code
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t result;
for (int i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>= 1;
}
return result;
}
};