ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
unsigned v = 0x01020304u, total = 0;
for (int i = 0; i < 4; i++) {
unsigned b = (v >> (8 * i)) & 0xFFu;
while (b) { total += b & 1u; b >>= 1; }
}
printf("%u", total);
}- A. 4
- B. 7
- C. 5
- D. 6
Correct answer: C. 5
The four bytes of 0x01020304 are 4, 3, 2 and 1, whose set-bit counts are 1, 2, 1 and 1. Their total is 5.
More C Programming questions
- What is printed? Compute the XOR of all integers from 1 to 1000 with this program. c #include <stdio.h> int main(void) { unsigned x = 0…
- How many monotone lattice paths go from the top-left corner to the bottom-right corner of a 4 by 4 grid of cells (moves only right or…
- How many iterations does this loop run? c #include <stdio.h> int main(void) { int c = 0; for (int i = 10; i >= 0; i -= 3) c++…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (int)'x41' + '101'); }
- What is printed? Assume int is 32 bits. c #include <stdio.h> struct B { unsigned a : 3; }; int main(void) { struct B s; s.a = 13…
- What is printed? Assume unsigned long long is 64 bits. c #include <stdio.h> int main(void) { unsigned long long v = 0xFFULL << 56…