位运算

求二进制中的1。
突然发现得转c++

#include<iostream>
using namespace std;


int Countone(long long x){
    int res=0;
    while(x){
        res += x & 1;
        x = x >> 1;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    long long n,x;
    cin >> n;
    for (int i = 0; i < n; i ++ ){
        cin >> x;
        cout << Countone(x) << " ";
    }
    
    return 0;
}
posted @ 2025-07-22 17:27  .N1nEmAn  阅读(8)  评论(0)    收藏  举报