Count the number of 1’s in the binary expression of a given integer
Example:
input: 90
output: 4
用位操作。
public int countOnes(int x) {
int count = 0;
while (x != 0) {
x &= x - 1;
count++;
}
return count;
}