ProgrammingHardCompetitive coding (CP)
How many loop iterations run? Compute with the program.
How many loop iterations run? Compute with the program.
#include <stdio.h>
int main(void) {
unsigned x = 0xF00D;
int c = 0;
while (x) {
x >>= 3;
c++;
}
printf("%d", c);
}- A. 6
- B. 4
- C. 16
- D. 11
Correct answer: A. 6
Each x >>= 3 drops three bits; 0xF00D = 61453 needs 16 bits, so it takes ceil(16 / 3) = 6 shifts to reach 0. 4 treats each shift as removing a hex digit, 16 shifts one bit at a time, and 11 divides by 3 instead of by 8.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int i = 5; do { i++; } while (i < 3); printf("%d", i); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { printf("%d %zu", 1 ? 'A' : 0, sizeof(1 ? 'A' : 0)); }
- What is printed? Assume unsigned long long is 64 bits. c #include <stdio.h> int main(void) { unsigned long long x = (1ULL << 40) - 1; int…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3}; int p = a; p += 2; printf("%d", p - (p - 2)); }
- What is printed? Assume unsigned short is 16 bits and int is 32 bits. c #include <stdio.h> int main(void) { printf("%d", (unsigned…
- How many binary strings of length 4 contain no two consecutive 1s? Compute with this recursive program. c #include <stdio.h> int f(int n)…