ProgrammingHardCompetitive coding (CP)

What is printed? Assume unsigned long long is 64 bits.

What is printed? Assume unsigned long long is 64 bits.
#include <stdio.h>
int main(void) {
    unsigned long long x = (1ULL << 40) - 1;
    int c = 0;
    for (; x; x &= x - 1) c++;
    printf("%d", c);
}
  1. A. 39
  2. B. 41
  3. C. 30
  4. D. 40

Correct answer: D. 40

(1 << 40) - 1 has forty consecutive set bits, and each loop step clears the lowest one.

More C Programming questions

Exams that ask this topic