ProgrammingHardWipro · Cognizant pattern

What is printed?

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

Correct answer: D. 16

flags starts 0, sets bit2 (0b00100=4), sets bit4 (0b10100=20), then clears bit2 via AND with the complement of (1<<2): 20 & ~4 = 0b10100 & 0b...11011 = 0b10000 = 16.

More C Programming questions

Exams that ask this topic