ProgrammingHardCompetitive coding (CP)

What is printed? Assume unsigned int is 32 bits.

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

Correct answer: B. 2147483648

Reversing a 32-bit word that holds 1 moves its bit to the top position, 2^31 = 2147483648.

More C Programming questions

Exams that ask this topic