Why should bit shift first? Thanks


  • 0
    W

    public int reverseBits(int n) {
    int answer=0;
    for(int i=0;i<32;i++){
    answer=answer<<1;//correct
    answer|=(n&1);
    n=n>>1;
    answer=answer<<1;//wrong
    }
    return answer;
    }


  • 1
    C

    You will do 32 left shift operation.
    So the most significant bit is moved out of the scope.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.