Why isn't this solution working? Reference: GFG


#1
unsigned int Solution::reverse(unsigned int n) {
    unsigned int rev=0;
    
    while(n>0){
        //left shift rev
        rev<<=1;
        
        if(n&1==1){
            //make last bit as 1
            rev^=1;
        }
        
        //right shift n
        n>>=1;
    }
    
    return rev;
}

#2

unsigned int Solution::reverse(unsigned int n) {
unsigned int rev=0;
int count=0;
while(n>0){
// count the no of bits in n
count++;
//left shift rev
rev<<=1;

    if(n&1==1){
        //make last bit as 1
        rev^=1;
    }
    
    //right shift n
    n>>=1;
}

// left shift upto 32 bits number
return rev<<(32-count);

}