Java programming — Unsigned right shift (>>>) of Integer.MIN_VALUE and printing before/after: class BitShift { public static void main(String [] args) { int x = 0x80000000; System.out.print(x + " and "); x = x >>> 31; System.out.println(x); } }

Difficulty: Easy

Correct Answer: -2147483648 and 1

Explanation:


Introduction / Context:
This problem demonstrates Java's unsigned right shift operator (>>>) and how it differs from arithmetic right shift (>>), especially with negative numbers.



Given Data / Assumptions:

  • x starts as 0x80000000 (most significant bit set), which is Integer.MIN_VALUE (decimal -2147483648).
  • Then x is shifted unsigned right by 31 bits.


Concept / Approach:
Unsigned right shift inserts zeros on the left. Shifting MIN_VALUE (1000...0) by 31 positions yields 0000...0001.



Step-by-Step Solution:

Initial print shows -2147483648 (the decimal form of 0x80000000) followed by " and ".After x = x >>> 31, x becomes 1.The final println prints 1, giving "-2147483648 and 1".


Verification / Alternative check:
Try arithmetic right shift (>>) to see that it would yield -1 by sign extension, highlighting the difference.



Why Other Options Are Wrong:
They either show hex formatting (not what System.out prints here) or results of signed shift, not unsigned.



Common Pitfalls:
Confusing >>> with >> and expecting sign extension instead of zero fill.



Final Answer:
-2147483648 and 1

More Questions from Operators and Assignments

Discussion & Comments

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