#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Return 1 when x contains an odd number of 1s; 0 otherwise.
Assume w=32. */
int odd_ones(unsigned x){
int y = x;
int count_ones = 0;
for (int i = 0; i < 31; i++) {
if (y & 1)
count_ones++;
y = y >> 1;
}
return count_ones&1;
}
int main() {
int x = 0x55555557;
printf("x=%x\n", x);
printf("odd_ones %x", odd_ones(x));
return 0;
}
位运算
最新推荐文章于 2021-05-28 13:12:04 发布