#include <stdio.h> #include <stdlib.h> int main() { int a = 20; int i; int count = 0; for (i = 0; i < 32; i++) { if (((a >>i)&1) == 1) count++; } printf("%d\n", count); system("pause"); return 0; }
运行结果:
#include <stdio.h> #include <stdlib.h> int main() { int a = 20; int count = 0; while (a) { if (a%2==1) count++; a /= 2; } printf("%d\n", count); system("pause"); return 0; }
求一个函数返回参数的二进制中1的个数
最新推荐文章于 2024-05-08 16:32:15 发布