Converting from Integer to Binary not working for 2147483648


  • 0
    C

    This code is failing for the input "2147483648" . I am converting the integer to binary with short division. Can I know how to fix this..

    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int hWeight = 0;
            while(n > 0){
                int reminder = n % 2;
                if(reminder == 1){
                    hWeight++;
                }
                n = n / 2;
            }
            return hWeight; 
        }
    }
    

Log in to reply
 

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