π One-stop destination for all your technical interview Preparation π
Reverse bits of a given 32 bits unsigned integer.
Note:
n&1
will give us lsb.31-i
bits and it will be our reverseLsb.class Solution{
public:
uint32_t reverseBits(uint32_t n){
int res = 0;
for (int i = 0; i < 32; i++){
int lsb = n & 1;
int reverseLsb = lsb << (31 - i);
res |= reverseLsb;
n = n >> 1;
}
return res;
}
};