32位无符号整数中1的个数
4 int bitcount(unsigned int n)
5 {
6 n = n - ((n>>1)&033333333333) - ((n>>2)&011111111111);
7 n = (n + (n>>3))&030707070707;
8 n = n % 0x3f;
9 return n;
10 }
17 int bitcount(unsigned int n)
18 {
19 n = n - ((n>>1)&0x77777777) - ((n>>2)&0x33333333)-((n>>3)&0x11111111);
20 n = (n + (n>>4))&0xf0f0f0f;
21 n = n + ((n >> 8) & 0xf) + ((n >> 16) & 0xf) + ((n >> 24) & 0xf);
22 n = n & 0xff;
23 return n;
24 }
4 int bitcount(unsigned int n)
5 {
6 unsigned int tmp;
7 tmp = n & 0x33333333;
8 n = n - tmp;
9 n = (n >> 2) & 0x33333333;
10 tmp = tmp - ((tmp >> 1) & 0x33333333);
11 n = n - ((n >> 1) & 0x33333333);
12 tmp = tmp + n;
13 n = tmp + (tmp >> 16);
14 tmp = (n & 0xf) + ((n >> 4) & 0xf) +
15 ((n >> 8) & 0xf) + ((n >> 12) & 0xf);
16 tmp = tmp & 0xff;
17 return tmp;
18 }
int bitcount(unsigned int n)
{
unsigned x;
x = (n >> 1) & 0x77777777;
n = n - x;
x = (x >> 1) & 0x77777777;
n = n - x;
x = (x >> 1) & 0x77777777;
n = n -x;
x = (n + (n >> 16));
n = (n & 0xf) + ((n >> 4) & 0xf) + ((n >> 8) & 0xf) + ((n >> 12) & 0xf);
x = n & 0xff;
return x;
}