ProgrammingVery hardCompetitive coding (CP)

How many values m in 0..15 have bit 2 set and bit 0 clear? Compute with this program.

How many values m in 0..15 have bit 2 set and bit 0 clear? Compute with this program.
#include <stdio.h>
int main(void) {
    int c = 0;
    for (unsigned m = 0; m < 16; m++)
        if (((m >> 2) & 1u) && !(m & 1u)) c++;
    printf("%d", c);
}
  1. A. 4
  2. B. 2
  3. C. 6
  4. D. 8

Correct answer: A. 4

The masks with bit 2 set and bit 0 clear are 4, 6, 12 and 14, which is four values.

More C Programming questions

Exams that ask this topic