glibc
— Built-in Function: int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.
返回‘1’的个数。
int f(int s,int n) //s源数字,n是数字长度
{
int cnt = 0;
for(int i = 0;i < n;i++)
if(s&(1 << i))
cnt++;
return cnt;
}