Assuming a 2-byte (16-bit) int in C, what hexadecimal value is printed here?\n\n#include<stdio.h>\n\nint main()\n{\n printf("%x\n", -1 << 3);\n return 0;\n}\n\nPick the most appropriate result given typical two’s-complement behavior.

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:

  • int is 16 bits.
  • Two’s complement representation.
  • -1 has all bits set: 0xffff.
  • printf("%x") prints the underlying bit pattern in hexadecimal.


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

More Questions from Bitwise Operators

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion