23 lines
386 B
C

#include <assert.h>
#include <limits.h>
/* Return 1 when x contains an odd number of 1s; 0 otherwise.
Assume w=32 */
int odd_ones(unsigned x) {
x ^= x >> 16;
x ^= x >> 8;
x ^= x >> 4;
x ^= x >> 2;
x ^= x >> 1;
return x & 1;
}
int main() {
for (unsigned x = 0; ; x++) {
assert(odd_ones(x) == __builtin_parity(x));
if (x == UINT_MAX) {
break;
}
}
}