题解 | 求int型正整数在内存中存储时1的个数
求int型正整数在内存中存储时1的个数
https://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int a;
cin >> a;
int count =0;
while(a)
{
a &=(a-1);
count++;
}
cout << count;
}
// 64 位输出请用 printf("%lld")
a &=(a-1) 的作用是消掉最低位的1,位运算符(&, |, ^, ~, <<, >>)是直接对内存中的二进制位进行操作的,不需要手动转换。