ProgrammingVery hardCompetitive coding (CP)
How many times does the loop body run?
How many times does the loop body run?
#include <stdio.h>
int main(void) {
unsigned mask = 0x2D, n = 0;
for (unsigned s = mask; s; s = (s - 1) & mask)
n++;
printf("%u", n);
}- A. 16
- B. 14
- C. 15
- D. 32
Correct answer: C. 15
The loop visits every non-empty submask of 0x2D, which has four set bits, so 2^4 - 1 = 15 submasks. The zero submask is excluded because the loop stops at s = 0.
More C Programming questions
- Assume unsigned int is 32 bits. What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint32t a = 70000, b = 70000…
- What is printed? c #include <stddef.h> #include <stdio.h> int main(void) { sizet n = 3; int k = -2; printf("%zu %d", (sizet)(k + n), (n +…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 0x06; uint8t r = 0; for (int i = 0; i < 8; i++) { r…
- What is printed? c #include <stdio.h> unsigned g(unsigned n) { return n ^ (n >> 1); } int main(void) { printf("%u %u", g(10), g(7)); }
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 12; uint8t y = (uint8t)-x; printf("%d", y); }
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint32t x = 0x00F0u; printf("%u", x & (0u - x)); }