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 = 32, b1 = (x >> 1) & 1u, b5 = (x >> 5) & 1u;
    if (b1 != b5) x ^= (1u << 1) | (1u << 5);
    printf("%u", x);
}
  1. A. 32
  2. B. 2
  3. C. 34
  4. D. 66

Correct answer: B. 2

Bit 5 of 32 is set and bit 1 is clear, so the code flips both bits, giving 32 XOR (2 | 32) = 2. The single set bit moves from position 5 to position 1.

More C Programming questions

Exams that ask this topic