ProgrammingVery hardProduct company OA
What is printed?
What is printed?
unsigned x = 0xF0F0;
x = x - ((x >> 1) & 0x5555);
x = (x & 0x3333) + ((x >> 2) & 0x3333);
x = (x + (x >> 4)) & 0x0F0F;
x = (x + (x >> 8)) & 0x1F;
printf("%u", x);- A. 16
- B. 4
- C. 8
- D. 240
Correct answer: C. 8
This is the parallel (SWAR) bit count for 16 bits: it sums bits in pairs, then nibbles, then bytes, then the two bytes. 0xF0F0 has 8 bits set. The masks keep each partial sum inside its own field, so the steps never interfere.
More C Programming questions
- What is printed? c struct N { int v, next; } t[] = {{10, 2}, {20, -1}, {30, 4}, {40, 1}, {50, 1}}; int s = 0; for (int i = 0; i != -1; i =…
- Given int x; const int pc; int p; int const cp = &x;, which assignment is a constraint violation that the compiler must diagnose?
- What is printed? c #define S(x) #x printf("%zu %zu", strlen(S("hi")), sizeof(S(ab)));
- What is printed? c int paths(int r, int c) { return (r == 0 || c == 0) ? 1 : paths(r - 1, c) + paths(r, c - 1); } int main(void) {…
- Assume 8-bit unsigned char. What is printed? c int n = 0; for (unsigned char c = 0; c < 300; c++) { n++; if (n > 1000) break; }…
- Assume IEEE-754 float (24-bit significand) and 32-bit int. What is printed? c int big = 16777217; / 2^24 + 1 / float f = big; printf("%d"…