求整数n的二进制表达式的 从低往高第k位 (k从0开始)
bit_k = (n>>k) & 1;
求整数n的二进制表达式最低的1 m = n & ~n;
原:1011000 1000000
反:0100111 0111111
补:0100111 1000000
原&补; 0000000 1000000
#include <iostream>
using namespace std;
const int N = 100010;
int a[N],cnt[N];
int main(){
int n,temp,lowbit;
cin >> n;
for(int i = 0 ; i < n ; i ++) cin >> a[i];
for(int i = 0 ; i < n ; i ++){
temp = a[i];
while(temp != 0){
//每次与1相位与 , 相当于取出二进制的最低位
lowbit = temp & 1;
if(lowbit == 1)
cnt[i] ++;
temp = temp >> 1; //temp = temp/2; 要习惯这么写
}
}
for(int i = 0 ; i < n ; i ++) cout << cnt[i]<<" ";
}