输入一个无符号整数,统计该整数中含1的比特数。
#include<stdio.h> int numbits(unsigned int n) { int count = 0; while(n>0){ n &= (n-1);//每次操作去掉n中最低位的1 count++; } return count; } int main() { unsigned int n; scanf("%d",&n); printf("%d\n",numbits(n)); return 0; }
原文地址:http://blog.csdn.net/crazy_xiaohe/article/details/39226483