public class Solution {
// You need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
// We should use n != 0 rather than n > 0
while(n != 0){
// Extract the least significant bit
count += (n & 1);
// Unsigned Right shift one bit to avoid extra 1 when n is negative
n = n >>> 1;
}
return count;
}
}