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));
}
  1. A. 1 0
  2. B. 0 1
  3. C. 1 1
  4. 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

Exams that ask this topic