ProgrammingHardTCS NQT · Wipro

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 = (unsigned)-5;
    printf("%u", x >> 31);
}
  1. A. 0
  2. B. 2
  3. C. 4
  4. D. 1

Correct answer: D. 1

-5 converted to unsigned is 2^32 - 5, which has its top bit set, so shifting right by 31 leaves 1.

More C Programming questions

Exams that ask this topic