Why this one create wrong answer?


  • 0
    W
    uint32_t reverseBits(uint32_t n) {
            uint32_t res;
            if(n&1==0) res=0;
            else res=1;
            
            for(int i=1;i<32;i++){
                res=(res<<1)|((n>>i)&1);
            }
            return res;
     }
    

    If I made some modification on the if statement, it is correct. Can anyone tell me why this happened? Thanks.

    if(n&1==1) res=1;
    else res=0;


  • 0
    T

    Check out Operator precedence table, and you should find out why.


Log in to reply
 

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