ProgrammingHardTCS NQT · Infosys pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int x = 12;
printf("%d %d %d", x & 1, x | 1, x ^ 1);
  1. A. 1 13 13
  2. B. 0 13 13
  3. C. 0 12 12
  4. D. 1 12 13

Correct answer: B. 0 13 13

12 is 0b1100. Bit 0 of 12 is 0, so x & 1 = 0 (even check). x | 1 forces bit 0 on: 0b1101 = 13. x ^ 1 flips bit 0: since it was 0, it becomes 1 too: 0b1101 = 13. Output: 0 13 13.

More C Programming questions

Exams that ask this topic