ProgrammingHardTCS NQT · Infosys pattern

Assume 32-bit int. What is printed?

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

Correct answer: A. 32

Setting bit 2 and bit 5 gives 0b100100 = 36. Then &= ~(1<<2) clears bit 2 specifically (ANDing with everything-except-bit-2), leaving only bit 5 set: 0b100000 = 32.

More C Programming questions

Exams that ask this topic