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);
}- A. 0
- B. 1
- C. 1001
- 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
- How many monotone lattice paths go from the top-left corner to the bottom-right corner of a 4 by 4 grid of cells (moves only right or…
- How many iterations does this loop run? c #include <stdio.h> int main(void) { int c = 0; for (int i = 10; i >= 0; i -= 3) c++…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (int)'x41' + '101'); }
- What is printed? Assume int is 32 bits. c #include <stdio.h> struct B { unsigned a : 3; }; int main(void) { struct B s; s.a = 13…
- What is printed? Assume unsigned long long is 64 bits. c #include <stdio.h> int main(void) { unsigned long long v = 0xFFULL << 56…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned long long n = 4294967290ULL; unsigned b =…