ProgrammingVery hardCompetitive coding (CP)
What is printed? The program checks whether the low four bits of each argument form a binary palindrome.
What is printed? The program checks whether the low four bits of each argument form a binary palindrome.
#include <stdio.h>
unsigned rev4(unsigned x) {
unsigned r = 0;
for (int i = 0; i < 4; i++) {
r = (r << 1) | (x & 1u);
x >>= 1;
}
return r;
}
int main(void) {
printf("%u %u", (unsigned)(rev4(9) == 9), (unsigned)(rev4(10) == 10));
}- A. 1 0
- B. 0 1
- C. 1 1
- D. 0 0
Correct answer: A. 1 0
9 is 1001, which reverses to itself, while 10 = 1010 reverses to 0101 = 5, so the second test fails.
More C Programming questions
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 1, r = 0; for (int i = 0; i < 32; i++)…
- What is printed? c #include <stdio.h> int main(void) { unsigned n = 1000; int p = -1; while (n) { n >>= 1; p++; } printf("%d", p); }
- 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[] =…