ProgrammingHardTCS NQT · Infosys pattern

What is printed? Assume 32-bit int, two's complement.

What is printed? Assume 32-bit int, two's complement.
unsigned int a = 5, b = 9;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%u %u", a, b);
  1. A. 9 5
  2. B. 5 9
  3. C. 0 14
  4. D. 14 0

Correct answer: A. 9 5

This is the classic XOR-swap idiom. After the three XOR steps a and b end up swapped without a temp: a becomes 9 (old b) and b becomes 5 (old a).

More C Programming questions

Exams that ask this topic