publicclassSolution{ // you need to treat n as an unsigned value publicinthammingWeight(int n){ int count = 0; String str = Integer.toBinaryString(n); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '1') { count++; } } return count; } }
publicclassSolution{ // you need to treat n as an unsigned value publicinthammingWeight(int n){ int count = 0; while (n != 0) { count++; n = n & (n - 1); } return count; } }