ProgrammingHardCompetitive coding (CP)

How many numbers from 1 to 100 have bit 3 set (value 8)? Compute with this program.

How many numbers from 1 to 100 have bit 3 set (value 8)? Compute with this program.
#include <stdio.h>
int main(void) {
    int c = 0;
    for (unsigned n = 1; n <= 100; n++)
        if (n >> 3 & 1u) c++;
    printf("%d", c);
}
  1. A. 50
  2. B. 49
  3. C. 48
  4. D. 52

Correct answer: C. 48

Bit 3 is set in blocks of 8 numbers that alternate with blocks of 8 that are clear, so each 16-number period contributes 8 and 1 to 100 gives 48.

More C Programming questions

Exams that ask this topic