Difficulty: Medium
Correct Answer: fff8
Explanation:
Introduction / Context:
This tests bit-level reasoning and formatting with %x when left-shifting a negative value. Although left-shifting negative signed integers is technically undefined by the C standard, many exam contexts expect two’s-complement reasoning on 16-bit ints.
Given Data / Assumptions:
Concept / Approach:
Treating -1 as 0xffff, a left shift by 3 moves all 1s three positions left, filling the lowest three bits with zeros. That is 0xffff << 3 → 0xfff8 (if we ignore undefined-behavior concerns and assume wrap within 16-bit representation).
Step-by-Step Solution:
-1 (16-bit) = 0xffff.Shift left by 3: 0xffff << 3 = 0xfff8.Printed in hexadecimal → fff8.
Verification / Alternative check:
On implementations where int is 16-bit and two’s complement, compiling similar code often shows fff8. If int is wider or the compiler enforces UB strictly, results may vary; this exam assumes the common two’s-complement interpretation.
Why Other Options Are Wrong:
ffff: no shift would be required to get this.0 / -1: do not match the shifted bit pattern.
Common Pitfalls:
Forgetting exam assumptions; mixing signed arithmetic rules with bitwise representation in %x.
Final Answer:
fff8
Discussion & Comments