Code review request for 6371401: java.math.BigInteger.shift(Integer.MIN_VALUE) throws StackOverflowError

Joseph D. Darcy Joe.Darcy at Sun.COM
Tue Oct 20 17:22:42 UTC 2009


Dmitry Nadezhin wrote:
> I want to make comments to this change from point of view of the post
> "BigInteger.bitLength() can return negative results".
>
> Suppose that the range of valid BigInteger is restiricted either to
> [-2^Integer.MAX_VALUE , 2^Integer.MAX_VALUE-1]
> or to
> [-2^Integer.MAX_VALUE+1, 2^Integer.MAX_VALUE-1].

Which is not the case today because BigInteger allows the creation of 
partially overflowed values, values so large that various internal 
counters in the class overflow or otherwise misbehave.

> In this case the exact result of x.shiftRight(Integer.MIN_VALUE)
> will be 0 for x==0;
> will be out of BigInteger range for any x != 0.
>
> The exact result of x.shiftLeft(Integer.MIN_VALUE)
> will be 0 for any valid x >= 0;
> and will be -1 for any valid x < 0.
>
> So it seems to me that x.shiftLeft(Integer.MIN_VALUE) should never 
> throw ArithmeticException


> and x.shiftRight(Integer.MIN_VALUE) should throw ArithmeticException 
> only when x is nonzero.

That is arguably more correct, but in my estimation screening out 
MIN_VALUE as an unsupported value is reasonable.

If an when BigInteger is made robust in the face of huge values, these 
decisions and be reexamined.

-Joe



More information about the core-libs-dev mailing list