ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
unsigned n = 1000;
int p = -1;
while (n) { n >>= 1; p++; }
printf("%d", p);
}- A. 10
- B. 6
- C. 9
- D. 512
Correct answer: C. 9
p starts at -1, so after the 10 shifts that 1000 needs (its bit length) it holds 9 = floor(log2 1000). 10 forgets the -1 start, 6 counts the set bits of 1000, and 512 is the highest power of two rather than its exponent.
More C Programming questions
- Would the product of 50000 and 50000 overflow a 32-bit int? The program checks with a 64-bit product. What is printed? c #include…
- What is printed? c #include <stdio.h> int main(void) { const char s = "112233"; int count[10] = {0}; for (const char p = s; p; p++)…
- What is the trace of the matrix product A B, where A = {{1, 2}, {3, 4}} and B = {{5, 6}, {7, 8}}? Compute with this program. c #include…
- How many elements of {0, 2, 1, 3, 4, 7, 6} equal their own index? Count with this program. c #include <stdio.h> int main(void) { int a[] =…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {2, 7, 1}; int e = a + sizeof a / sizeof a[0] - 1; printf("%d", e); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int a[4]; printf("%ld", (long)((char )&a[2] - (char…