ProgrammingVery hardProduct company OA

Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed?

Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed?
unsigned int a = 0x80000001u;
unsigned int b = a * 2;
int c = (int)(a >> 31);
unsigned int d = (a << 1) >> 1;
unsigned long long e = (unsigned long long)a << 1;
printf("%u %d %u %llu", b, c, d, e);
  1. A. 2 1 2147483649 4294967298
  2. B. 2 -1 1 2
  3. C. 2 1 1 4294967298
  4. D. 4294967298 1 1 4294967298

Correct answer: C. 2 1 1 4294967298

Unsigned arithmetic is modulo 2³²: a·2 = 0x100000002 → 2. a >> 31 on an unsigned value is a logical shift → 1. (a << 1) already lost the top bit (it is evaluated in 32 bits) so shifting back gives 1, not the original a. Only when a is widened BEFORE the shift does the carry survive: 4294967298. The option with d = 2147483649 assumes the compiler 'remembers' the bit shifted out.

More C Programming questions

Exams that ask this topic