ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
unsigned char x = 0x1D, r = 0;
for (int i = 0; i < 8; i++) {
    r = (r << 1) | (x & 1);
    x >>= 1;
}
printf("%#x", r);
  1. A. 0xd1
  2. B. 0x8b
  3. C. 0xb1
  4. D. 0xb8

Correct answer: D. 0xb8

Each pass shifts r left and copies x's lowest bit in, then drops that bit from x, so the 8 bits come out in reverse order. 0x1D is 0001 1101; reversed it is 1011 1000 = 0xb8. 0xd1 just swaps the two hex digits, which is a different operation. %#x prints the 0x prefix.

More C Programming questions

Exams that ask this topic