//整数n的二进制表示中1的个数
int NumberOf1(int n){
int cnt = 0;
while (n != 0){
cnt++;
n = n&(n - 1);
}
return cnt;
}
测试
int n = -1;
cout << n << " has 1 is " << NumberOf1(n) << endl;
//整数n的二进制表示中1的个数
int NumberOf1(int n){
int cnt = 0;
while (n != 0){
cnt++;
n = n&(n - 1);
}
return cnt;
}
测试
int n = -1;
cout << n << " has 1 is " << NumberOf1(n) << endl;