#include "stdio.h"int count_one_bits(unsigned int value){  int count=0;  while(value)  {  if(value%2==1)  {  count++;  }  value=value/2;  }  return count;}int main(){unsigned int num=0;int ret=0;scanf("%d",&num);ret=count_one_bits(num);printf("count=%d\n",ret);return 0;}

优化:

#include "stdio.h"int main(){ int num=15; int count=0; while(num) {  if(num%2==1)  {   count++;  }     num=num/2; } printf("%d\n",count); system("pause");}

输入15,得出结果count=4