ProgrammingVery hardCompetitive coding (CP)
What is printed? The program inverts the Gray code 6 by XOR-folding its bits.
What is printed? The program inverts the Gray code 6 by XOR-folding its bits.
#include <stdio.h>
int main(void) {
unsigned g = 6, n = 0;
for (; g; g >>= 1) n ^= g;
printf("%u", n);
}- A. 4
- B. 6
- C. 5
- D. 2
Correct answer: A. 4
The loop XORs 6, 3 and 1, giving 6 ^ 3 ^ 1 = 4. That is the binary value whose Gray code is 6.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { unsigned char c = 200; if (c 2 > 300) puts("big"); else puts("small"); }
- What is the difference between the largest and the smallest element of {4, -2, 9, 0}, found in a single pass with pointers? Compute it…
- How many pairs (i, j) with i < j in {1, 3, 5, 8, 10} have a difference of exactly 2 (that is, a[j] - a[i] = 2)? Compute with this program…
- Starting at node 0, how many nodes does this linked walk visit before it reaches the sentinel -1? The next-index array is {3, 2, 0, -1}. c…
- What is the digital root of 987654 (the repeated digit sum until one digit remains)? The recursion computes it. What is printed? c…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 5; printf("%d", (x - 6) > 0); }