ProgrammingHardTCS NQT · Infosys

What is 0 ^ 1 ^ 2 ^ ... ^ 31, the XOR of all integers from 0 to 31?

  1. A. 31
  2. B. 0
  3. C. 32
  4. D. 1

Correct answer: B. 0

Prefix XOR of 0..n has the value 0 when n mod 4 is 3, and 31 mod 4 is 3. Pairs of equal bit patterns cancel, so the result is 0.

More C Programming questions

Exams that ask this topic