Difficulty: Medium
Correct Answer: ffff
Explanation:
Introduction / Context:
This question probes your understanding of right-shifting negative signed integers on a 16-bit implementation, and of how printf prints hexadecimal using %x. The key idea is arithmetic right shift on two’s-complement values.
Given Data / Assumptions:
Concept / Approach:
An arithmetic right shift fills in with the sign bit (1 for negative numbers). Shifting 0xFFFF right by any number of positions preserves all ones. Therefore, -1 >> 1 remains 0xFFFF when interpreted as a 16-bit signed arithmetic shift. Printing with %x emits lowercase hexadecimal without leading 0x.
Step-by-Step Solution:
Represent -1 → 0xFFFF.Perform arithmetic shift right by 1 → 0xFFFF (ones shift in).printf("%x", 0xFFFF) → prints ffff.
Verification / Alternative check:
Many compilers document that right shift of negative signed integers is implementation-defined, but on mainstream targets it is arithmetic. If it were a logical shift, the result would be 0x7FFF; the question’s 16-bit assumption targets the common arithmetic behavior.
Why Other Options Are Wrong:
0fff and fff0 do not correspond to a one-bit arithmetic right shift of all ones. 0000 is impossible for -1 >> 1 under arithmetic shifting. 7fff would be a logical right shift, not typical for signed.
Common Pitfalls:
Confusing logical vs arithmetic shifts; forgetting integer size assumptions; overlooking that %x prints unsigned representation of the resulting bit pattern.
Final Answer:
ffff
Discussion & Comments