AcWing算法基础课---第一讲基础算法---05位运算

### 整数n的二进制数的第k位数
```
n >> k & 1
```

### lowbit运算
```
lowbit(x)
x & (~x + 1) = x & (-x)
```

### AcWing 801. 二进制中1的个数
```
#include <iostream>

using namespace std;

int lowbit(int x)
{
return x & -x;
}

int main()
{
int n;
cin >> n;

while (n --)
{
int x;
cin >> x;

int res = 0;
while (x) x -= lowbit(x), res ++;

cout << res << " ";
}

return 0;
}

```

posted @ 2022-08-23 15:09  hjy94wo  阅读(18)  评论(0)    收藏  举报