/*
***求取十进制数字元素1的个数
*/int fun(int x)
{int count = 0;int i, j, k;/***方法2 负数不可计算,需要改进*/while (x != 0){if (x & 1 == 1){count++;}x = x >> 1;}/****方法1*/while (x != 0){x = x&(x - 1);count++;}return count;
}int main()
{int k;int num = 0;scanf("%d", &k);num = fun(k);printf("%d", num);system("pause");return 0;}
运算过程:
https://blog.csdn.net/csdn_kou/article/details/79764559