basics · intermediate · ~15 min
Return whether the number of set bits is odd.
Implement:
int parity(unsigned x);
Return 1 if x has an odd number of set bits, else 0.
x.
1 for odd popcount, else 0.
None.
#include <stddef.h>
/* Return 1 if x has an odd number of set bits, else 0. */
int parity(unsigned x){ (void)x; return 0; }
Returning the popcount itself instead of its low bit.
0 -> 0; 7 (three bits) -> 1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.