ACM L: XueXX and Binary

L: XueXX and Binary


Description

XueXX is a clever boy. And he always likes numbers in binary(二進制). What an interesting hobby!
Now XueXX has some number in decimal(十進制), he wants to know how many “1”s in a number if the number is written in binary.

Input

The first line of input contains the number of test cases (T<=100). The descriptions of the test cases follow: The first line of each test case contains one integer a (0<a<=1,000,000,000), standing for the number in decimal.

Output

For each test case, output a single line containing the result.

Sample Input

3
1
91
735

Sample Output

1
5
8
代碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
//平行算法(計算二進制的1 的個數)
int bitcount(unsigned int n)
{
    n=(n&0x55555555)+((n>>1)&0x55555555);
    n=(n&0x33333333)+((n>>2)&0x33333333);
    n=(n&0x0f0f0f0f)+((n>>4)&0x0f0f0f0f);
    n=(n&0x00ff00ff)+((n>>8)&0x00ff00ff);
    n=(n&0x0000ffff)+((n>>16&0x0000ffff));
    return n;
}
int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {

        scanf("%d",&n);
       // cout<<"sdfas";
        printf("%d\n",bitcount(n));
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章