ProgrammingHardGATE CS · TCS NQT

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint16_t x = (uint16_t)~0x00F0u;
    int c = 0;
    for (; x; x &= x - 1) c++;
    printf("%d", c);
}
  1. A. 12
  2. B. 8
  3. C. 28
  4. D. 4

Correct answer: A. 12

Truncating ~0x00F0 to 16 bits gives 0xFF0F, which has 12 set bits. Counting the bits of the original 0x00F0 gives 4, and the 32-bit complement would have 28.

More C Programming questions

Exams that ask this topic