1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| char a = -3; printf("a = %d\n", a);
int cnt = hammingWeight(a); printf("cnt = %d\n", cnt);
char b = a >> 1; printf("b = %d,a / 2 = %d\n", b, a / 2);
unsigned char c = 3; printf("c = %d\n", c);
unsigned char d = c >> 1; printf("d = %d\n", d);
int hammingWeight(int n) { int cnt = 0; while (n != 0) { cnt += 1; n &= n - 1; } return cnt; }
|