ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint64_t v = (1ULL << 40) | (1ULL << 3) | 1ULL;
    int c = 0;
    while (v) {
        v &= v - 1;
        c++;
    }
    printf("%d", c);
}
  1. A. 41
  2. B. 2
  3. C. 3
  4. D. 4

Correct answer: C. 3

Clearing the lowest set bit with v &= v - 1 is repeated until v is zero, and there are three set bits. Forgetting the &= gives a count that depends on the bit positions instead.

More C Programming questions

Exams that ask this topic