1//Method 1
2 int count = __builtin_popcount(number);
3//Method 2
4 int count = 0;
5 while (number) {
6 count += number & 1;
7 n >>= 1;
8 }
1int hammingWeight(uint32_t n) {
2 int count = 0;
3
4 while (n) {
5 n &= (n - 1);
6 count++;
7 }
8
9 return count;
10}
1//Method 1
2 int count = 0;
3 while (n)
4 {
5 count++;
6 n >>= 1;
7 }
8//Method 2
9 int count = (int)log2(number)+1;