ProgrammingVery hardGATE CS · Infosys

Assume uint16t is 16 bits, unsigned int is 32 bits and int is 32 bits. What is printed?

Assume uint16_t is 16 bits, unsigned int is 32 bits and int is 32 bits. What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint16_t h = 0xFFF0;
    uint32_t w = h << 4;
    uint16_t k = h >> 4;
    uint32_t m = (uint32_t)-8 >> 1;
    printf("%X %X %X", (unsigned)w, (unsigned)k, (unsigned)m);
}
  1. A. FFF0 FFF 7FFFFFFC
  2. B. FFF00 FFF 7FFFFFFC
  3. C. FFF00 FFF FFFFFFFC
  4. D. FFF00 FFF 3FFFFFFE

Correct answer: B. FFF00 FFF 7FFFFFFC

h is promoted to int before the shift, so h << 4 keeps the high digit (0xFFF00), and (uint32_t)-8 is 0xFFFFFFF8 whose right shift by one is 0x7FFFFFFC. Forgetting the widening gives FFF0, and skipping the shift leaves FFFFFFFC.

More C Programming questions

Exams that ask this topic