/****求取十进制数字元素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;}
运算过程:
/csdn_kou/article/details/79764559
如果觉得《输出该数二进制表示中1的个数。求取十进制数字元素1的个数 (3种方法)》对你有帮助,请点赞、收藏,并留下你的观点哦!