标签:
方法一: #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() { int ret = 0; ret = count_one_bits(15); printf("%d\n",ret); //printf("%d\n",count_one_bits(ret)); return 0; } 方法二: #include<stdio.h> int count_one_bits_1(unsigned int value) { int count = 0; while(value) { count ++; value = value &(value - 1); } return count; } int main() { int ret = 0; ret = count_one_bits_1(15); printf("%d\n",ret); //printf("%d\n",count_one_bits(ret)); return 0; } 方法三: #include<stdio.h> int count_one_bits_2(unsigned int value) { int count = 0; while(value) { if(value%2 == 1) { count ++; value =value>>1; } } return count; } int main() { int ret = 0; ret = count_one_bits_2(15); printf("%d\n",ret); //printf("%d\n",count_one_bits(ret)); return 0; }
版权声明:本文博主原创文章,博客,未经同意不得转载。
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/4815982.html