Use bit manipulation to count set bits.
n & 1 extracts rightmost bitn & (n-1) clears rightmost 1 bit (faster)1public class Solution {
2 public int hammingWeight(int n) {
3 int count = 0;
4
5 while (n != 0) {
6 count += n & 1;
7 n >>>= 1;
8 }
9
10 return count;
11 }
12}