Java BigInteger to toUnsignedBigInteger(long i)

Here you can find the source of toUnsignedBigInteger(long i)

Description

Return a BigInteger equal to the unsigned value of the argument.

License

Open Source License

Declaration

private static BigInteger toUnsignedBigInteger(long i) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigInteger;

public class Main {
    /**/*from   w  w  w .  j  a  va 2 s .com*/
     * Return a BigInteger equal to the unsigned value of the
     * argument.
     */
    private static BigInteger toUnsignedBigInteger(long i) {
        if (i >= 0L)
            return BigInteger.valueOf(i);
        else {
            int upper = (int) (i >>> 32);
            int lower = (int) i;

            // return (upper << 32) + lower
            return (BigInteger.valueOf(toUnsignedLong(upper))).shiftLeft(32)
                    .add(BigInteger.valueOf(toUnsignedLong(lower)));
        }
    }

    /**
     * Converts the argument to a {@code long} by an unsigned
     * conversion.  In an unsigned conversion to a {@code long}, the
     * high-order 32 bits of the {@code long} are zero and the
     * low-order 32 bits are equal to the bits of the integer
     * argument.
     *
     * Consequently, zero and positive {@code int} values are mapped
     * to a numerically equal {@code long} value and negative {@code
     * int} values are mapped to a {@code long} value equal to the
     * input plus 2<sup>32</sup>.
     *
     * @param  x the value to convert to an unsigned {@code long}
     * @return the argument converted to {@code long} by an unsigned
     *         conversion
     * @since 1.8
     */
    public static long toUnsignedLong(int x) {
        return ((long) x) & 0xffffffffL;
    }
}

Related

  1. toPushbackReader(BigInteger x)
  2. toStr(BigInteger number)
  3. toString(BigInteger bi)
  4. toString(BigInteger[] vector)
  5. toUnsigned(final BigInteger[] ulong)
  6. toUnsignedBigInteger(long number)