ProgrammingHardGATE CS · Campus placements

uint8_t a = 0x3C, b = 0xA5; the statements a ^= b; b ^= a; a ^= b; run in that order. What are a and b afterwards?

  1. A. a = 0x3C, b = 0xA5
  2. B. a = 0x99, b = 0x99
  3. C. a = 0xA5, b = 0x3C
  4. D. a = 0x00, b = 0xFF

Correct answer: C. a = 0xA5, b = 0x3C

After the first step a holds 0x99 (the XOR of the two values). The second step restores b to 0x3C and the third restores a to 0xA5, so the values swap without a temporary.

More C Programming questions

Exams that ask this topic