Question about "int" and "long"


  • 0
    M
    public int reverseBits(int n) {
    int sum = 0;
    for(int i = 0; i < 32; i++){
    sum += Math.pow(2, 31 - i) * (n & 1);
    n = n >> 1;
    }
    return sum;
    }
    

    "Can anybody help me to explain why I cannot use "int" to clarify "sum"? If I change "int" to "long", I could past the test; if not, the error is like this: Output:
    2147483647 (01111111111111111111111111111111)
    Expected:
    2147483648 (10000000000000000000000000000000)

    Thanks a lot!"


Log in to reply
 

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