Posted on 2014-07-19 12:12
xLsDg 阅读(172)
评论(0) 编辑 收藏 引用 所属分类:
代码库
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int BitCount1( unsigned int n )
5 {
6 unsigned int c = 0;
7
8 for ( c =0; n; n >>= 1 )
9 c += n & 1;
10
11 return c ;
12 }
13
14 int BitCount2( unsigned int num )
15 {
16 int count = 0;
17
18 while ( num ) {
19 count++;
20 num = num & ( num - 1 );
21 }
22
23 return count;
24 }
25
26 int main()
27 {
28 unsigned int num = 99;
29
30 printf("%d has %d\n", num, BitCount2( num ) );
31
32 return 0;
33 }
34