ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
int s = 0;
for (unsigned i = 0; i < 8; i++)
s += (i ^ (i >> 1)) & 1u;
printf("%d", s);
}- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: A. 4
The lowest bit of the Gray code of i is set for 4 of the 8 values, since the Gray code's bit 0 equals bit 0 XOR bit 1 of i. So the sum is 4.
More C Programming questions
- What is printed? Assume int is 32 bits. c #include <stdio.h> int main(void) { int a = -3; unsigned b = 2; printf("%u", a b); }
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 7; printf("%u", a / 2 2); }
- What is printed? c #include <stdio.h> int f(int n) { static int memo[10]; if (n < 2) return n; if (memo[n]) return memo[n]; return memo[n]…
- What is printed? c #include <stdio.h> int main(void) { printf("%c", 'a' + ('z' - 'a') / 2); }
- How many trailing one bits does 0x1F3 have? Compute with this program. c #include <stdio.h> int main(void) { unsigned x = 0x1F3; int c =…
- What is printed? c #include <stdio.h> int main(void) { unsigned long long p = 0xFFFFu 0xFFFFu; printf("%llu", p); }