ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint32_t v = 0x00F0F0A5u;
v = v - ((v >> 1) & 0x55555555u);
v = (v & 0x33333333u) + ((v >> 2) & 0x33333333u);
v = (v + (v >> 4)) & 0x0F0F0F0Fu;
v = (v * 0x01010101u) >> 24;
printf("%u", v);
}- A. 12
- B. 10
- C. 8
- D. 14
Correct answer: A. 12
This is the SWAR popcount: each step adds neighbouring bit counts, and the multiply by 0x01010101 sums the four byte counts into the top byte. The three bytes 0xF0, 0xF0 and 0xA5 hold 4 + 4 + 4 = 12 set bits.
More C Programming questions
- What is printed? c #include <stdio.h> #include <string.h> #include <stdlib.h> int cmp(const void a, const void b) { const char x = (const…
- What is printed? c #include <stdio.h> int main(void) { int m[2][3] = {{1, 2, 3}, {4, 5, 6}}, t[3][2]; for (int i = 0; i < 2; i++) for (int…
- What is printed? c #include <stdio.h> int calls = 0; unsigned g(unsigned a, unsigned b) { calls++; return b == 0 ? a : g(b, a % b); } int…
- What is printed? Assume unsigned int is 32 bits and unsigned long long is 64 bits. c #include <stdio.h> unsigned long long pw(unsigned b…
- What is printed? c #include <stdio.h> void bits(unsigned n) { if (n > 1) bits(n / 2); putchar('0' + n % 2); } int main(void) { bits(11); }
- What is printed? Assume uint16t is 16 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint16t a = 300, b = 300; uint32t p…