Java BigInteger From getUnsignedBigInteger(long x)

Here you can find the source of getUnsignedBigInteger(long x)

Description

Ensure that the passed long parameter will be interpreted as a positive value.

License

Open Source License

Parameter

Parameter Description
x a parameter

Declaration

public static final BigInteger getUnsignedBigInteger(long x) 

Method Source Code

//package com.java2s;

import java.math.BigInteger;

public class Main {
    /**/*  w  w w .ja  v  a2s  .c  o  m*/
     * 2^64
     */
    final static BigInteger x64 = BigInteger.ONE.shiftLeft(64);

    /**
     * Ensure that the passed long parameter will be interpreted as a positive value.
     * @param x
     */
    public static final BigInteger getUnsignedBigInteger(long x) {
        BigInteger z = BigInteger.valueOf(x);
        if (z.signum() < 0)
            return z.add(x64);
        else
            return z;
    }

    /**
     * Ensure that the passed long parameter will be interpreted as a positive value.
     * 
     * @param x - value.
     * @param bitWidth - VM word size.
     * @return a positive value.
     */
    public static final BigInteger getUnsignedBigInteger(long x, int bitWidth) {
        return BigInteger.valueOf(x & (((long) 1 << bitWidth) - 1));
    }
}

Related

  1. bytesToBigInteger(byte[] data)
  2. bytesToBigInteger(byte[] data, int[] offset)
  3. convertToBigInteger(String s)
  4. convertToBigInteger(String sequence)
  5. getUnsignedBigInteger(byte[] data, int offset, int length)
  6. string2BigInteger(String plaintext)
  7. toBigInteger(@Nullable final Boolean value, @Nullable final BigInteger defaultValue)
  8. toBigInteger(@Nullable final byte[] value)
  9. ToBigInteger(BitSet bs)