ProgrammingVery hardCompetitive coding (CP)

What is printed? Compute the XOR of all integers from 1 to 1000 with this program.

What is printed? Compute the XOR of all integers from 1 to 1000 with this program.
#include <stdio.h>
int main(void) {
    unsigned x = 0;
    for (unsigned i = 1; i <= 1000; i++) x ^= i;
    printf("%u", x);
}
  1. A. 0
  2. B. 1
  3. C. 1001
  4. D. 1000

Correct answer: D. 1000

The XOR of 1 to n depends on n mod 4, and for n = 1000 (divisible by 4) it equals n itself.

More C Programming questions

Exams that ask this topic