Using simple bits operations


#1

unsigned int Solution::reverse(unsigned int n) {
int d=log2(n)+1;
unsigned int res=0;
int p=d;
if(n==0) return 0;
while(p–){
int q=n&1;
n=n>>1;
res=res<<1;
res=res|q;
}
while(d<32){
res=res<<1;
d++;
}
return res;
}