输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数。
C++代码
#include<iostream>
using namespace std;
int main()
{
int n;
int cnt=0;
cin>>n;
while(n)
{
n = n & (n-1);
cnt++;
}
cout<< cnt <<endl;
return 0;
}
python 代码
#!/usr/bin/env python
n=int(raw_input())
cnt =0
while n:
n = n & (n-1)
cnt=cnt+1;
print cnt