ProgrammingHardIBM · Deloitte pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
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 gives 0b00100=4. Setting bit 4 gives 0b10100=20. Clearing bit 2 with &= ~(1<<2) removes it: 0b10000=16. This is the standard set/clear-bit idiom.

More C Programming questions

Exams that ask this topic