ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
unsigned x = 0xDEAD;
int c = 0;
unsigned t = x & 0xAAAA;
while (t) { c += t & 1u; t >>= 1; }
printf("%d", c);
}- A. 5
- B. 35496
- C. 6
- D. 11
Correct answer: C. 6
0xDEAD & 0xAAAA keeps only the odd-position bits, giving 0x8AA8 = 1000 1010 1010 1000, which has 6 set bits. 5 is the count for the even mask 0x5555, 11 counts all bits of 0xDEAD, and 35496 is the masked value itself, not its popcount.
More C Programming questions
- How many comparisons does bsearch-style binary search make to find the key 6 in the sorted array 0 to 9? Compute with the program. c…
- What is printed? c #include <stdio.h> int main(void) { int s = 0; for (unsigned i = 0; i < 8; i++) s += (i ^ (i >> 1)) & 1u; printf("%d"…
- 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); }