JDK 8 code review request for initial unsigned integer arithmetic library support
Eamonn McManus
eamonn at mcmanus.net
Thu Jan 19 06:43:18 UTC 2012
Ulf Zibis writes:
> What about:
> private static final BigInteger BEYOND_UNSIGNED_LONG =
BigInteger.valueOf(1).**shiftLeft(64);
> private static BigInteger toUnsignedBigInteger(long i) {
> BigInteger result = BigInteger.valueOf(i);
> if (i < 0L)
> result = result.add(BEYOND_UNSIGNED_**LONG);
> return result;
> }
That's a nice idea! But the problem is that it would mean that
BigInteger.class would be loaded as soon as Long.class is, which I think is
undesirable. However it does make me think that we could change this:
if (i >= 0L)
return BigInteger.valueOf(i);
else {
int upper = (int) (i >>> 32);
int lower = (int) i;
// return (upper << 32) + lower
return
(BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
}
to this:
if (i >= 0L) {
return BigInteger.valueOf(i);
} else {
return BigInteger.valueOf(i & Long.MAX_VALUE).setBit(63);
}
Éamonn
On 18 January 2012 19:52, Ulf Zibis <Ulf.Zibis at gmx.de> wrote:
> Am 18.01.2012 03:54, schrieb Joe Darcy:
>
> I've posted a revised webrev at
>>
>> http://cr.openjdk.java.net/~**darcy/4504839.2<http://cr.openjdk.java.net/~darcy/4504839.2>
>>
>
> Instead
> <code>'\u0030'</code>
> you can use
> {@code '\u005Cu0030'}
>
> Byte:
> =====
> 459 public static int toUnsignedInt(byte x) {
> 460 return ((int) x) & 0xff;
> 461 }
> This should be good enough (similar at Short, Integer):
> 459 public static int toUnsignedInt(byte x) {
> 460 return x & 0xff;
> 461 }
> (This notation if regularly used in sun.nio.cs coders.)
>
> missing:
> public static short toUnsignedShort(byte x)
>
> superfluous:
> public static long toUnsignedInt(byte x)
> public static long toUnsignedLong(byte x) (similar at Short)
> one can use:
> int i = toUnsignedShort(x)
> long l = toUnsignedShort(x) (similar at Short)
>
> Integer:
> ========
> 623 * <li>The value represented by the string is larger than the
> 624 * largest unsigned {@code int}, 2<sup>32</sup>-1.
> If you format {@code int}, then you speak about the java type int, which
> is always signed, never unsigned.
> IMO you should better write 'unsigned 32-bit integer".
> (similar at Long)
>
> 598 * Parses the string argument as an unsigned integer in the radix
> 599 * specified by the second argument.
> IMHO, there should be a note about what happens on values above 2^31 - 1.
>
> 672 * Parses the string argument as an unsigned decimal integer. The
> 673 * characters in the string must all be decimal digits, except
> Better, like lines 598ff, or contrariwise (similar at Long):
> 672 * Parses the string argument as an unsigned decimal integer.
> 673 *
> 674 * The characters in the string must all be decimal digits, except
>
> Long:
> =====
> What about:
> private static final BigInteger BEYOND_UNSIGNED_LONG =
> BigInteger.valueOf(1).**shiftLeft(64);
> private static BigInteger toUnsignedBigInteger(long i) {
> BigInteger result = BigInteger.valueOf(i);
> if (i < 0L)
> result = result.add(BEYOND_UNSIGNED_**LONG);
> return result;
> }
>
> Instead
> private static BigInteger toUnsignedBigInteger(long i)
> at class BigInteger we more generally could have:
> public static BigInteger unsignedValueOf(long i)
>
> 610 * Parses the string argument as an unsigned {@code long} in the
> 611 * radix specified by the second argument.
> IMHO, there should be a note about what happens on values above 2^63 - 1.
>
> -Ulf
>
>
More information about the core-libs-dev
mailing list