class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while (n){
count++;
n&=n-1;
}
return count;
}
};
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n > 0){
count++;
n &= (n - 1);
}
return count;
}
}
I tried in java, but 2147483648 did not pass. Is there a difference btw java & C++ here? Or my code has a bug? Thank you.