function bitParity(int x) {
x ^= x >> 16;
x ^= x >> 8;
x ^= x >> 4;
x &= 0xf;
return (0x6996 >> x) & 1;
}
Finding odd number of bits in an integer
nt
leftmost_one (unsigned x){ x |= (x >> 1 ); x |= (x >> 2 ); x |= (x >> 4 ); x |= (x >> 8 ); x |= (x >> 16 );
return x^(x>> 1 ); nt
leftmost_one (unsigned x){ x |= (x >> 1 ); x |= (x >> 2 ); x |= (x >> 4 ); x |= (x >> 8 ); x |= (x >> 16 );
return x^(x>> 1 ); int leftmost_one(unsigned x){x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); x |= (x >> 8); x |= (x >> 16); return x^(x>>1); }
Generate mask indicating leftmost 1 in x
int lower_one_mask(int n){ return (2<<(n-1)) - 1; } or { return ~(~0 << n); }
Mask with least signficant n bits set to 1
|