// 统计一个数二进制中的1的个数#includeint count(int a){ int count = 0; while (a) { count++; a = a & (a - 1); } return count;}int main(){ printf("%d\n", count(10)); printf("%d\n", count(0)); printf("%d\n", count(-1)); return 0;}
本文共 229 字,大约阅读时间需要 1 分钟。
// 统计一个数二进制中的1的个数#includeint count(int a){ int count = 0; while (a) { count++; a = a & (a - 1); } return count;}int main(){ printf("%d\n", count(10)); printf("%d\n", count(0)); printf("%d\n", count(-1)); return 0;}