ProgrammingHardWipro · Cognizant pattern

What is printed? (Assume int is 32 bits, two's complement, arithmetic right shift for signed negatives — the common but implementation-defined behaviour.)

What is printed? (Assume int is 32 bits, two's complement, arithmetic right shift for signed negatives — the common but implementation-defined behaviour.)
int a = -8;
int b = a >> 1;
printf("%d", b);
  1. A. 2147483644
  2. B. -4
  3. C. 4
  4. D. -16

Correct answer: B. -4

Right-shifting a negative signed int is implementation-defined, but virtually all mainstream compilers (GCC/Clang/MSVC) perform an ARITHMETIC shift, replicating the sign bit: -8 >> 1 = -4 (equivalent to dividing by 2, rounding toward negative infinity for this value).

More C Programming questions

Exams that ask this topic