find the number of 1 bits

LeetCode 191

1
2
3
4
5
6
7
8
9
10
11
int hammingWeight(uint32_t n) {
int count = 0
for(int i = 0; i < 32; i++){
int origin = n
int comp = origin^1;
if(origin > comp)
count++
n >>= 1
}
return count
}

LeetCode 525