ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint8_t x = 0x06;
    uint8_t r = 0;
    for (int i = 0; i < 8; i++) {
        r = (uint8_t)((r << 1) | (x & 1));
        x >>= 1;
    }
    printf("%d", r);
}
  1. A. 192
  2. B. 96
  3. C. 6
  4. D. 24

Correct answer: B. 96

Reading the bits of 00000110 from the low end and pushing them into r reverses the 8-bit pattern, giving 01100000 = 96. Skipping the loop returns 6.

More C Programming questions

Exams that ask this topic