ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
unsigned inv(unsigned g) {
unsigned n = 0;
for (; g; g >>= 1)
n ^= g;
return n;
}
int main(void) {
printf("%u", inv(13));
}- A. 13
- B. 8
- C. 9
- D. 11
Correct answer: C. 9
This loop computes the prefix XOR of the bits, which inverts the Gray code. For 13 it gives 13 ^ 6 ^ 3 ^ 1 = 9, and the Gray code of 9 is 9 ^ 4 = 13.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int dp[16] = {0}; int sum = 0; for (int i = 1; i < 16; i++) dp[i] = dp[i >> 1] + (i…
- Assume int is 4 bytes with 4-byte alignment and char is 1 byte. What is printed? c #include <stdio.h> struct A { char c; int i; char d; }…
- How many subsets of {1, 2, 3, 4} (the empty set included) have an XOR of zero? c #include <stdio.h> int main(void) { int count = 0; for…
- What is printed? Assume int is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t b = (uint8t)-200; printf("%d"…
- What is printed? c #include <stdio.h> int main(void) { char s[] = "abcdef"; char p = s; int n = 0; while (p && p - s < 4) n += p++ - 'a'…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = (int const )a; const int y =…