ProgrammingHardCompetitive coding (CP)
How many subsets of the bit positions of mask 22 (binary 10110) are visited, counting the empty subset? Compute with this program.
How many subsets of the bit positions of mask 22 (binary 10110) are visited, counting the empty subset? Compute with this program.
#include <stdio.h>
int main(void) {
unsigned mask = 22, s = mask;
int cnt = 0;
for (;;) {
cnt++;
if (s == 0) break;
s = (s - 1) & mask;
}
printf("%d", cnt);
}- A. 8
- B. 7
- C. 9
- D. 16
Correct answer: A. 8
mask 22 has three set bits, so it has 2^3 = 8 submasks including 0. The loop visits each of them once, ending when it reaches 0.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 0xB6, b = 0x3C; printf("%u", a & ~b); }
- What is printed? This program tries every subset of {3, 5, 6} and prints the largest XOR of a subset (the empty subset counts as 0). c…
- What is printed? c #include <stdio.h> char g[3][4] = {"110", "010", "011"}; int fill(int r, int c) { if (r < 0 || c < 0 || r >= 3 || c >=…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = a, y = b; int sx = x[0] +…
- What is printed? c #include <stdio.h> #include <stdlib.h> struct Item { int id; char tag; }; int cmp(const void k, const void e) { const…
- What is printed? c #include <stdio.h> int digitsum(unsigned long long n) { return n == 0 ? 0 : (int)(n % 10) + digitsum(n / 10); } int…