Why this java solution does not work


  • 0
    A
    public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        String s = Integer.toString(n,2);
        int j = 0;
        int l = s.length();
        for(int i = 0; i < l; i++){
            char c = s.charAt(l-i-1);
            int k = Character.getNumericValue(c);
            j += (k<<(32-i-1));
        }
        return j;
    }
    

    }

    Input:
    2147483648 (10000000000000000000000000000000)
    Output:
    2147483649 (10000000000000000000000000000001)
    Expected:
    1 (00000000000000000000000000000001)

    I do not know why there is a '1' in the end, could someone tell me why?


Log in to reply
 

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