1. Representation of negative numbers in the computer
In the computer, both positive and negative numbers are represented by two's complement.
2. How to find the kth digit in the binary representation of n
3. lowbit(x): returns the last bit of x 1
4. Example: the number of 1 in binary
Given a sequence of length n , please find the number of 1s in the binary representation of each number in the sequence .
input format
The first line contains the integer n .
The second line contains n integers representing the entire array.
output format
A total of one line, containing n integers, where the i -th number represents the number of 1s in the binary representation of the i -th number in the sequence .
data range
1≤n≤100000,
0≤The value of the element in the array≤10 9
Input sample:
5
1 2 3 4 5
Sample output:
1 1 2 1 2
code:
#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++;//每次减去x的最后一位1
cout<<res<<' ';
}
return 0;
}