ProgrammingHardTCS NQT · Accenture

What is printed? Assume uint16t is 16 bits.

What is printed? Assume uint16_t is 16 bits.
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint16_t x = 0x0003, r = 0;
    for (int i = 0; i < 16; i++) {
        r = (uint16_t)((r << 1) | (x & 1u));
        x >>= 1;
    }
    printf("%d", r);
}
  1. A. 24576
  2. B. 32768
  3. C. 49152
  4. D. 49153

Correct answer: C. 49152

Reading the 16 bits of 0x0003 from the low end and pushing them into r reverses the whole word, giving 0xC000 = 49152. Reversing only the low byte would give 0x00C0.

More C Programming questions

Exams that ask this topic