ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned x = 3, r = 0;
    for (int i = 0; i < 4; i++) {
        r = (r << 1) | (x & 1u);
        x >>= 1;
    }
    printf("%u", r);
}
  1. A. 12
  2. B. 3
  3. C. 6
  4. D. 9

Correct answer: A. 12

Reversing the four bits 0011 gives 1100, which is 12. Stopping after two iterations would return 3.

More C Programming questions

Exams that ask this topic