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);
}
  1. A. 8
  2. B. 7
  3. C. 9
  4. 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

Exams that ask this topic