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);
}- A. 1
- B. 2
- C. 3
- 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
- What is printed? Assume int is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t b = (uint8t)-200; printf("%d"…
- What is printed? c #include <stdio.h> int main(void) { char s[] = "abcdef"; char p = s; int n = 0; while (p && p - s < 4) n += p++ - 'a'…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = (int const )a; const int y =…
- What is printed? c #include <stdio.h> int main(void) { int m[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) m[i][j] = i 3…
- How many times is disk 3 moved in this recursive Towers of Hanoi with 5 disks? Compute the program output. c #include <stdio.h> int cnt =…
- How many integers n with 1 <= n <= 64 satisfy both (n & (n - 1)) == 0 and (n & 0x55555555u) != 0? Compute with this program. c #include…