ProgrammingHardTCS NQT · Infosys pattern

What is printed? Assume 32-bit int.

What is printed? Assume 32-bit int.
int flags = 0;
flags |= (1 << 2);
flags |= (1 << 4);
flags &= ~(1 << 2);
printf("%d", flags);
  1. A. 16
  2. B. 20
  3. C. 4
  4. D. 12

Correct answer: A. 16

Setting bit 2 and bit 4 gives 0b10100 = 20, then clearing bit 2 with &= ~(1<<2) removes that bit, leaving only bit 4 set: 0b10000 = 16.

More C Programming questions

Exams that ask this topic