给定一个整数,求它的二进制表示中有多少个1。


1
#include<stdio.h> 2 3 int countOne( int n ) 4 { 5 int count = 0; 6 while( n != 0 ) 7 { 8 n &= n - 1; 9 ++count; 10 } 11 return count; 12 } 13 14 int main() 15 { 16 int num; 17 printf("Input the number : "); 18 scanf( "%d", &num ); 19 printf( "The number of binary one is %d.\n", countOne(num) ); 20 return 0; 21 }
原文地址:https://www.cnblogs.com/shuanghong/p/3001653.html