ProgrammingHardCompetitive coding (CP)

How many subsets of {1, 2, 3, 4} (the empty set included) have an XOR of zero?

How many subsets of {1, 2, 3, 4} (the empty set included) have an XOR of zero?
#include <stdio.h>
int main(void) {
    int count = 0;
    for (unsigned m = 0; m < 16; m++) {
        unsigned x = 0;
        for (unsigned i = 0; i < 4; i++)
            if (m >> i & 1u) x ^= i + 1;
        if (x == 0) count++;
    }
    printf("%d", count);
}
  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 4

Correct answer: B. 2

The two subsets with XOR zero are the empty set and {1, 2, 3}, since 1 ^ 2 ^ 3 = 0. The set {1, 2, 3, 4} has XOR 4, so it does not count.

More C Programming questions

Exams that ask this topic