Java : BigInteger with Examples

BigInteger (Java SE 17 & JDK 17) API Examples.
You will find code examples on most BigInteger methods.


Summary

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types).

Class diagram

// 1000 + 234
final var integer = new BigInteger("1000");
final var val = new BigInteger("234");

final var ret = integer.add(val);
System.out.println(ret); // 1234
// An example with big numbers.
final var integer = new BigInteger("111111111122222222223333333333");
final var val = new BigInteger("111111111111111111111111111111");

final var ret = integer.add(val);
System.out.println(ret); // 222222222233333333334444444444

Examples on this page use the helper method below.

// Prints a value as two's complement.
public void printBinary(BigInteger integer) {

    for (final var b : integer.toByteArray()) {
        final var s = "%8s".formatted(Integer.toBinaryString(b & 0xff));
        System.out.print(s.replace(' ', '0') + " ");
    }
    System.out.println();
}
printBinary(new BigInteger(new byte[]{(byte) 0x00})); // 00000000
printBinary(new BigInteger(new byte[]{(byte) 0xff})); // 11111111
printBinary(new BigInteger(new byte[]{(byte) 0xff, (byte) 0x00})); // 11111111 00000000
printBinary(new BigInteger(new byte[]{(byte) 0xaa, (byte) 0xaa})); // 10101010 10101010

printBinary(new BigInteger("7")); // 00000111
printBinary(new BigInteger("-7")); // 11111001

Fields

static final BigInteger ONE

The BigInteger constant one.

System.out.println(BigInteger.ONE); // 1

static final BigInteger TEN

The BigInteger constant ten.

System.out.println(BigInteger.TEN); // 10

static final BigInteger TWO

The BigInteger constant two.

System.out.println(BigInteger.TWO); // 2

static final BigInteger ZERO

The BigInteger constant zero.

System.out.println(BigInteger.ZERO); // 0

Constructors

BigInteger (byte[] val)

Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.

final byte[] val = {(byte) 0x7f};
final var integer = new BigInteger(val);

System.out.println(integer); // 127
System.out.println(integer.toString(16)); // 7f
final byte[] val = {(byte) 0xff};
final var integer = new BigInteger(val);

System.out.println(integer); // -1
System.out.println(integer.toString(16)); // -1
final byte[] val = {(byte) 0x7f, (byte) 0xff};
final var integer = new BigInteger(val);

System.out.println(integer); // 32767
System.out.println(integer.toString(16)); // 7fff
final byte[] val = {(byte) 0x7f, (byte) 0xff, (byte) 0x00};
final var integer = new BigInteger(val);

System.out.println(integer); // 8388352
System.out.println(integer.toString(16)); // 7fff00
final byte[] val = {(byte) 0xff, (byte) 0x00};
final var integer = new BigInteger(val);

System.out.println(integer); // -256
System.out.println(integer.toString(16)); // -100

BigInteger (byte[] val, int off, int len)

Translates a byte sub-array containing the two's-complement binary representation of a BigInteger into a BigInteger.

Please see also : BigInteger(byte[] val)

final byte[] val = {(byte) 0x7f, (byte) 0x01, (byte) 0x02, (byte) 0x03};

System.out.println(new BigInteger(val, 0, 4).toString(16)); // 7f010203
System.out.println(new BigInteger(val, 0, 3).toString(16)); // 7f0102
System.out.println(new BigInteger(val, 0, 2).toString(16)); // 7f01
System.out.println(new BigInteger(val, 0, 1).toString(16)); // 7f

System.out.println(new BigInteger(val, 0, 4).toString(16)); // 7f010203
System.out.println(new BigInteger(val, 1, 3).toString(16)); // 10203
System.out.println(new BigInteger(val, 2, 2).toString(16)); // 203
System.out.println(new BigInteger(val, 3, 1).toString(16)); // 3

BigInteger (int signum, byte[] magnitude)

Translates the sign-magnitude representation of a BigInteger into a BigInteger.

final byte[] val = {(byte) 0xff, (byte) 0xff};

final var integer1 = new BigInteger(val);
System.out.println(integer1); // -1
System.out.println(integer1.toString(16)); // -1

final var integer2 = new BigInteger(1, val);
System.out.println(integer2); // 65535
System.out.println(integer2.toString(16)); // ffff

final var integer3 = new BigInteger(-1, val);
System.out.println(integer3); // -65535
System.out.println(integer3.toString(16)); // -ffff

// NumberFormatException: signum-magnitude mismatch
//new BigInteger(0, val);
final byte[] val = {(byte) 0x00};

final var integer = new BigInteger(0, val);
System.out.println(integer); // 0

BigInteger (int signum, byte[] magnitude, int off, int len)

Translates the sign-magnitude representation of a BigInteger into a BigInteger.

Please see also : BigInteger(int signum, byte[] magnitude)

final byte[] val = {(byte) 0xff, (byte) 0x01, (byte) 0x02, (byte) 0x03};

System.out.println(new BigInteger(1, val, 0, 4).toString(16)); // ff010203
System.out.println(new BigInteger(1, val, 0, 3).toString(16)); // ff0102
System.out.println(new BigInteger(1, val, 0, 2).toString(16)); // ff01
System.out.println(new BigInteger(1, val, 0, 1).toString(16)); // ff

System.out.println(new BigInteger(-1, val, 0, 4).toString(16)); // -ff010203
System.out.println(new BigInteger(-1, val, 1, 3).toString(16)); // -10203
System.out.println(new BigInteger(-1, val, 2, 2).toString(16)); // -203
System.out.println(new BigInteger(-1, val, 3, 1).toString(16)); // -3

BigInteger (int bitLength, int certainty, Random rnd)

Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength.

Please see also : probablePrime(int bitLength, Random rnd)

final var rnd = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var integer = new BigInteger(4, 1, rnd);
    System.out.println(integer);
});

// Result
// ↓
//11
//13
//13
//13
//11
final var rnd = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var integer = new BigInteger(8, 10, rnd);
    System.out.println(integer);
});

// Result
// ↓
//251
//131
//229
//157
//229

BigInteger (int numBits, Random rnd)

Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive.

final var rnd = new Random();

IntStream.range(0, 10).forEach(i -> {
    final var integer = new BigInteger(8, rnd);
    System.out.println(integer.toString(16));
});

// Result
// ↓
//8d
//81
//73
//58
//65
//5e
//e0
//68
//45
//72
final var rnd = new Random();

IntStream.range(0, 10).forEach(i -> {
    final var integer = new BigInteger(16, rnd);
    System.out.println(integer.toString(16));
});

// Result
// ↓
//88a1
//6144
//559e
//93db
//6519
//abef
//6d18
//5e9b
//eaee
//e907

BigInteger (String val)

Translates the decimal String representation of a BigInteger into a BigInteger.

final var integer1 = new BigInteger("1234");
System.out.println(integer1); // 1234

final var integer2 = new BigInteger("-5678");
System.out.println(integer2); // -5678
final var bigInteger = new BigInteger("1234567890123456789012345678901234567890");
System.out.println(bigInteger); // 1234567890123456789012345678901234567890

BigInteger (String val, int radix)

Translates the String representation of a BigInteger in the specified radix into a BigInteger.

Decimal : BigInteger(String val)

// Hexadecimal
final var integer1 = new BigInteger("1234abcd", 16);
System.out.println(integer1); // 305441741
System.out.println(integer1.toString(16)); // 1234abcd

final var integer2 = new BigInteger("-ffff", 16);
System.out.println(integer2); // -65535
System.out.println(integer2.toString(16)); // -ffff
// Binary
final var integer1 = new BigInteger("11110000", 2);
System.out.println(integer1); // 240
System.out.println(integer1.toString(2)); // 11110000
System.out.println(integer1.toString(16)); // f0

final var integer2 = new BigInteger("-1111111111111111", 2);
System.out.println(integer2); // -65535
System.out.println(integer2.toString(2)); // -1111111111111111
System.out.println(integer2.toString(16)); // -ffff

Methods

BigInteger abs ()

Returns a BigInteger whose value is the absolute value of this BigInteger.

final var integer = new BigInteger("1234");
System.out.println(integer); // 1234

final var ret = integer.abs();
System.out.println(ret); // 1234
final var integer = new BigInteger("-5678");
System.out.println(integer); // -5678

final var ret = integer.abs();
System.out.println(ret); // 5678

BigInteger add (BigInteger val)

Returns a BigInteger whose value is (this + val).

final var integer = new BigInteger("1000");
final var val = new BigInteger("234");

final var ret = integer.add(val);
System.out.println(ret); // 1234
final var integer = new BigInteger("100");
final var val = new BigInteger("-234");

final var ret = integer.add(val);
System.out.println(ret); // -134
final var integer = new BigInteger("111111111122222222223333333333");
final var val = new BigInteger("111111111111111111111111111111");

final var ret = integer.add(val);
System.out.println(ret); // 222222222233333333334444444444

BigInteger and (BigInteger val)

Returns a BigInteger whose value is (this & val).

final var integer = new BigInteger(new byte[]{(byte) 0x0f});
final var val = new BigInteger(new byte[]{(byte) 0xaa});
final var ret = integer.and(val);

printBinary(integer); // 00001111
printBinary(val);     // 10101010
printBinary(ret);     // 00001010

BigInteger andNot (BigInteger val)

Returns a BigInteger whose value is (this & ~val).

final var integer = new BigInteger(new byte[]{(byte) 0x0f});
final var val = new BigInteger(new byte[]{(byte) 0xaa});
final var ret = integer.andNot(val);

printBinary(integer); // 00001111
printBinary(val);     // 10101010
printBinary(ret);     // 00000101

int bitCount ()

Returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit.

final var integer = new BigInteger(new byte[]{0x00});
printBinary(integer); // 00000000
System.out.println(integer); // 0

System.out.println(integer.bitCount()); // 0
final var integer = new BigInteger(new byte[]{(byte) 0xff});
printBinary(integer); // 11111111
System.out.println(integer); // -1

System.out.println(integer.bitCount()); // 0
final var integer = new BigInteger(new byte[]{(byte) 0x7f});
printBinary(integer); // 01111111
System.out.println(integer); // 127

System.out.println(integer.bitCount()); // 7
final var integer = new BigInteger(new byte[]{(byte) 0x8a});
printBinary(integer); // 10001010
System.out.println(integer); // -118

System.out.println(integer.bitCount()); // 5

int bitLength ()

Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.

System.out.println(new BigInteger("0", 2).bitLength()); // 0
System.out.println(new BigInteger("1", 2).bitLength()); // 1
System.out.println(new BigInteger("10", 2).bitLength()); // 2
System.out.println(new BigInteger("100", 2).bitLength()); // 3
System.out.println(new BigInteger("1000", 2).bitLength()); // 4
System.out.println(new BigInteger("10000", 2).bitLength()); // 5
System.out.println(new BigInteger("1", 2).bitLength()); // 1
System.out.println(new BigInteger("11", 2).bitLength()); // 2
System.out.println(new BigInteger("111", 2).bitLength()); // 3
System.out.println(new BigInteger("1111", 2).bitLength()); // 4
System.out.println(new BigInteger("11111", 2).bitLength()); // 5

System.out.println(new BigInteger("10101", 2).bitLength()); // 5
final var integer = new BigInteger("-128");
printBinary(integer); // 10000000
System.out.println(integer.bitLength()); // 7
final var integer = new BigInteger("-129");
printBinary(integer); // 11111111 01111111
System.out.println(integer.bitLength()); // 8

byte byteValueExact ()

Converts this BigInteger to a byte, checking for lost information.

System.out.println(Byte.MAX_VALUE); // 127
System.out.println(Byte.MIN_VALUE); // -128

System.out.println(BigInteger.valueOf(0).byteValueExact()); // 0
System.out.println(BigInteger.valueOf(12).byteValueExact()); // 12
System.out.println(BigInteger.valueOf(127).byteValueExact()); // 127

// ArithmeticException: BigInteger out of byte range
//BigInteger.valueOf(128).byteValueExact();

System.out.println(BigInteger.valueOf(-34).byteValueExact()); // -34
System.out.println(BigInteger.valueOf(-128).byteValueExact()); // -128

// ArithmeticException: BigInteger out of byte range
//BigInteger.valueOf(-129).byteValueExact();

BigInteger clearBit (int n)

Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.

final var integer = new BigInteger(new byte[]{(byte) 0xff});
printBinary(integer); // 11111111

IntStream.range(0, 10).forEach(n -> {
    final var ret = integer.clearBit(n);
    System.out.print("n = " + n + " : ret = ");
    printBinary(ret);
});

// Result
// ↓
//n = 0 : ret = 11111110
//n = 1 : ret = 11111101
//n = 2 : ret = 11111011
//n = 3 : ret = 11110111
//n = 4 : ret = 11101111
//n = 5 : ret = 11011111
//n = 6 : ret = 10111111
//n = 7 : ret = 11111111 01111111
//n = 8 : ret = 11111110 11111111
//n = 9 : ret = 11111101 11111111

int compareTo (BigInteger val)

Compares this BigInteger with the specified BigInteger.

final var integer1 = BigInteger.valueOf(111);
final var integer2 = BigInteger.valueOf(222);

System.out.println(integer1.compareTo(integer2)); // -1
System.out.println(integer2.compareTo(integer1)); // 1
final var integer1 = BigInteger.valueOf(123);
final var integer2 = BigInteger.valueOf(-123);

System.out.println(integer1.compareTo(integer2)); // 1
System.out.println(integer2.compareTo(integer1)); // -1
final var integer1 = BigInteger.valueOf(123456789);
final var integer2 = BigInteger.valueOf(123456789);

System.out.println(integer1.compareTo(integer2)); // 0
System.out.println(integer2.compareTo(integer1)); // 0

BigInteger divide (BigInteger val)

Returns a BigInteger whose value is (this / val).

// 6 ÷ 2 = 3
final var integer = BigInteger.valueOf(6);

final var ret = integer.divide(BigInteger.TWO);
System.out.println(ret); // 3
// 13 ÷ 5 = 2 (remainder of 3)
final var integer = BigInteger.valueOf(13);
final var divisor = BigInteger.valueOf(5);

final var ret = integer.divide(divisor);
System.out.println(ret); // 2
// ArithmeticException: BigInteger divide by zero
//BigInteger.ONE.divide(BigInteger.ZERO);

BigInteger[] divideAndRemainder (BigInteger val)

Returns an array of two BigIntegers containing (this / val) followed by (this % val).

// 6 ÷ 2 = 3
final var integer = BigInteger.valueOf(6);

final var ret = integer.divideAndRemainder(BigInteger.TWO);
System.out.println(ret.length); // 2
System.out.println(ret[0]); // 3
System.out.println(ret[1]); // 0
// 13 ÷ 5 = 2 (remainder of 3)
final var integer = BigInteger.valueOf(13);
final var divisor = BigInteger.valueOf(5);

final var ret = integer.divideAndRemainder(divisor);
System.out.println(ret[0]); // 2
System.out.println(ret[1]); // 3
// ArithmeticException: BigInteger divide by zero
//BigInteger.ONE.divideAndRemainder(BigInteger.ZERO);

double doubleValue ()

Converts this BigInteger to a double.

System.out.println(BigInteger.valueOf(0).doubleValue()); // 0.0
System.out.println(BigInteger.valueOf(1234).doubleValue()); // 1234.0
System.out.println(BigInteger.valueOf(-5678).doubleValue()); // -5678.0
final var integer1 = new BigInteger("123456789012345678901234567890");
System.out.println(integer1.doubleValue()); // 1.2345678901234568E29

final var integer2 = integer1.pow(100);
System.out.println(integer2.doubleValue()); // Infinity

boolean equals (Object x)

Compares this BigInteger with the specified Object for equality.

final var integer1 = BigInteger.valueOf(111);
final var integer2 = BigInteger.valueOf(222);

System.out.println(integer1.equals(integer2)); // false
final var integer1 = BigInteger.valueOf(123);
final var integer2 = BigInteger.valueOf(-123);

System.out.println(integer1.equals(integer2)); // false
final var integer1 = new BigInteger("123456789");
final var integer2 = new BigInteger("123456789");

System.out.println(integer1 != integer2); // true
System.out.println(integer1.equals(integer2)); // true

BigInteger flipBit (int n)

Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.

final var integer = new BigInteger(new byte[]{(byte) 0xff});
printBinary(integer); // 11111111

IntStream.range(0, 10).forEach(n -> {
    final var ret = integer.flipBit(n);
    System.out.print("n = " + n + " : ret = ");
    printBinary(ret);
});

// Result
// ↓
//n = 0 : ret = 11111110
//n = 1 : ret = 11111101
//n = 2 : ret = 11111011
//n = 3 : ret = 11110111
//n = 4 : ret = 11101111
//n = 5 : ret = 11011111
//n = 6 : ret = 10111111
//n = 7 : ret = 11111111 01111111
//n = 8 : ret = 11111110 11111111
//n = 9 : ret = 11111101 11111111
final var integer = new BigInteger(new byte[]{0x00});
printBinary(integer); // 00000000

IntStream.range(0, 10).forEach(n -> {
    final var ret = integer.flipBit(n);
    System.out.print("n = " + n + " : ret = ");
    printBinary(ret);
});

// Result
// ↓
//n = 0 : ret = 00000001
//n = 1 : ret = 00000010
//n = 2 : ret = 00000100
//n = 3 : ret = 00001000
//n = 4 : ret = 00010000
//n = 5 : ret = 00100000
//n = 6 : ret = 01000000
//n = 7 : ret = 00000000 10000000
//n = 8 : ret = 00000001 00000000
//n = 9 : ret = 00000010 00000000

float floatValue ()

Converts this BigInteger to a float.

System.out.println(BigInteger.valueOf(0).floatValue()); // 0.0
System.out.println(BigInteger.valueOf(1234).floatValue()); // 1234.0
System.out.println(BigInteger.valueOf(-5678).floatValue()); // -5678.0
final var integer1 = new BigInteger("123456789012345678901234567890");
System.out.println(integer1.floatValue()); // 1.2345679E29

final var integer2 = integer1.pow(100);
System.out.println(integer2.floatValue()); // Infinity

BigInteger gcd (BigInteger val)

Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val).

final var integer = BigInteger.valueOf(12);
final var val = BigInteger.valueOf(18);

System.out.println(integer.gcd(val)); // 6
System.out.println(BigInteger.ZERO.gcd(BigInteger.ZERO)); // 0

int getLowestSetBit ()

Returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit).

System.out.println(new BigInteger("1").getLowestSetBit()); // 0
System.out.println(new BigInteger("10").getLowestSetBit()); // 1
System.out.println(new BigInteger("100").getLowestSetBit()); // 2
System.out.println(new BigInteger("1000").getLowestSetBit()); // 3
System.out.println(new BigInteger("10000").getLowestSetBit()); // 4

System.out.println(BigInteger.ZERO.getLowestSetBit()); // -1
System.out.println(new BigInteger("1000").getLowestSetBit()); // 3
System.out.println(new BigInteger("1001").getLowestSetBit()); // 0
System.out.println(new BigInteger("1010").getLowestSetBit()); // 1
System.out.println(new BigInteger("1100").getLowestSetBit()); // 2

int hashCode ()

Returns the hash code for this BigInteger.

System.out.println(BigInteger.valueOf(0).hashCode()); // 0
System.out.println(BigInteger.valueOf(12345).hashCode()); // 12345
System.out.println(BigInteger.valueOf(-45678).hashCode()); // -45678

System.out.println(new BigInteger("111111111122222222223333333333").hashCode()); // 35228486
System.out.println(new BigInteger("1234").hashCode()); // 1234
System.out.println(BigInteger.valueOf(1234).hashCode()); // 1234

int intValue ()

Converts this BigInteger to an int.

System.out.println(BigInteger.ZERO.intValue()); // 0
System.out.println(BigInteger.valueOf(1234).intValue()); // 1234
System.out.println(BigInteger.valueOf(-5678).intValue()); // -5678
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648

System.out.println(BigInteger.valueOf(2147483647L).intValue()); // 2147483647
System.out.println(BigInteger.valueOf(2147483648L).intValue()); // -2147483648
System.out.println(BigInteger.valueOf(2147483649L).intValue()); // -2147483647

System.out.println(BigInteger.valueOf(-2147483648L).intValue()); // -2147483648
System.out.println(BigInteger.valueOf(-2147483649L).intValue()); // 2147483647
System.out.println(BigInteger.valueOf(-2147483650L).intValue()); // 2147483646

int intValueExact ()

Converts this BigInteger to an int, checking for lost information.

System.out.println(BigInteger.ZERO.intValueExact()); // 0
System.out.println(BigInteger.valueOf(1234).intValueExact()); // 1234
System.out.println(BigInteger.valueOf(-5678).intValueExact()); // -5678
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648

System.out.println(BigInteger.valueOf(2147483647L).intValueExact()); // 2147483647
// ArithmeticException: BigInteger out of int range
//BigInteger.valueOf(2147483648L).intValueExact();

System.out.println(BigInteger.valueOf(-2147483648L).intValueExact()); // -2147483648
// ArithmeticException: BigInteger out of int range
//BigInteger.valueOf(-2147483649L).intValueExact();

boolean isProbablePrime (int certainty)

Returns true if this BigInteger is probably prime, false if it's definitely composite.

IntStream.range(0, 30).forEach(i -> {
    final var integer = BigInteger.valueOf(i);

    if (integer.isProbablePrime(10)) {
        System.out.println(integer);
    }
});

// Result
// ↓
//2
//3
//5
//7
//11
//13
//17
//19
//23
//29

long longValue ()

Converts this BigInteger to a long.

System.out.println(BigInteger.ZERO.longValue()); // 0
System.out.println(BigInteger.valueOf(1234).longValue()); // 1234
System.out.println(BigInteger.valueOf(-5678).longValue()); // -5678
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Long.MIN_VALUE); // -9223372036854775808

System.out.println(new BigInteger("9223372036854775807").longValue()); // 9223372036854775807
System.out.println(new BigInteger("9223372036854775808").longValue()); // -9223372036854775808
System.out.println(new BigInteger("9223372036854775809").longValue()); // -9223372036854775807

System.out.println(new BigInteger("-9223372036854775808").longValue()); // -9223372036854775808
System.out.println(new BigInteger("-9223372036854775809").longValue()); // 9223372036854775807
System.out.println(new BigInteger("-9223372036854775810").longValue()); // 9223372036854775806

long longValueExact ()

Converts this BigInteger to a long, checking for lost information.

System.out.println(BigInteger.ZERO.longValueExact()); // 0
System.out.println(BigInteger.valueOf(1234).longValueExact()); // 1234
System.out.println(BigInteger.valueOf(-5678).longValueExact()); // -5678
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Long.MIN_VALUE); // -9223372036854775808

System.out.println(new BigInteger("9223372036854775807").longValueExact()); // 9223372036854775807
// ArithmeticException: BigInteger out of long range
//new BigInteger("9223372036854775808").longValueExact();

System.out.println(new BigInteger("-9223372036854775808").longValueExact()); // -9223372036854775808
// ArithmeticException: BigInteger out of long range
//new BigInteger("-9223372036854775809").longValueExact();

BigInteger max (BigInteger val)

Returns the maximum of this BigInteger and val.

final var integer1 = BigInteger.valueOf(123);
final var integer2 = BigInteger.valueOf(456);

System.out.println(integer1.max(integer2)); // 456
System.out.println(integer2.max(integer1)); // 456
final var integer1 = BigInteger.valueOf(1234);
final var integer2 = BigInteger.valueOf(-1234);

System.out.println(integer1.max(integer2)); // 1234
System.out.println(integer2.max(integer1)); // 1234

BigInteger min (BigInteger val)

Returns the minimum of this BigInteger and val.

final var integer1 = BigInteger.valueOf(123);
final var integer2 = BigInteger.valueOf(456);

System.out.println(integer1.min(integer2)); // 123
System.out.println(integer2.min(integer1)); // 123
final var integer1 = BigInteger.valueOf(1234);
final var integer2 = BigInteger.valueOf(-1234);

System.out.println(integer1.min(integer2)); // -1234
System.out.println(integer2.min(integer1)); // -1234

BigInteger mod (BigInteger m)

Returns a BigInteger whose value is (this mod m).

final var integer = BigInteger.valueOf(13);
final var m = BigInteger.valueOf(5);

System.out.println(integer.mod(m)); // 3
System.out.println(integer.remainder(m)); // 3
final var integer = BigInteger.valueOf(-13);
final var m = BigInteger.valueOf(5);

System.out.println(integer.mod(m)); // 2
System.out.println(integer.remainder(m)); // -3
final var integer = BigInteger.valueOf(-13);
final var m = BigInteger.valueOf(-5);

// ArithmeticException: BigInteger: modulus not positive
//integer.mod(m);

System.out.println(integer.remainder(m)); // -3

BigInteger modInverse (BigInteger m)

Returns a BigInteger whose value is (this-1 mod m).

final var integer = BigInteger.valueOf(10);

IntStream.range(1, 10).forEach(val -> {
    final var m = BigInteger.valueOf(val);
    try {
        final var ret = integer.modInverse(m);
        System.out.println("m = " + m + " : ret = " + ret);
    } catch (ArithmeticException e) {
        System.out.println("m = " + m + " : " + e.getMessage());
    }
});

// Result
// ↓
//m = 1 : ret = 0
//m = 2 : BigInteger not invertible.
//m = 3 : ret = 1
//m = 4 : BigInteger not invertible.
//m = 5 : BigInteger not invertible.
//m = 6 : BigInteger not invertible.
//m = 7 : ret = 5
//m = 8 : BigInteger not invertible.
//m = 9 : ret = 1

BigInteger modPow (BigInteger exponent, BigInteger m)

Returns a BigInteger whose value is (thisexponent mod m).

final var integer = BigInteger.valueOf(10);

// 10 mod 4
final var ret1 = integer.modPow(BigInteger.valueOf(1), BigInteger.valueOf(4));
System.out.println(ret1); // 2

// 100 mod 8
final var ret2 = integer.modPow(BigInteger.valueOf(2), BigInteger.valueOf(8));
System.out.println(ret2); // 4

// 1000 mod 14
final var ret3 = integer.modPow(BigInteger.valueOf(3), BigInteger.valueOf(14));
System.out.println(ret3); // 6
final var integer = BigInteger.valueOf(10);
final var exponent = BigInteger.valueOf(-1);

IntStream.range(1, 10).forEach(val -> {
    final var m = BigInteger.valueOf(val);
    try {
        final var ret = integer.modPow(exponent, m);
        System.out.println("m = " + m + " : ret = " + ret);
    } catch (ArithmeticException e) {
        System.out.println("m = " + m + " : " + e.getMessage());
    }
});

// Result
// ↓
//m = 1 : ret = 0
//m = 2 : BigInteger not invertible.
//m = 3 : ret = 1
//m = 4 : BigInteger not invertible.
//m = 5 : BigInteger not invertible.
//m = 6 : BigInteger not invertible.
//m = 7 : ret = 5
//m = 8 : BigInteger not invertible.
//m = 9 : ret = 1

BigInteger multiply (BigInteger val)

Returns a BigInteger whose value is (this * val).

final var integer = BigInteger.valueOf(1234);
final var val = BigInteger.valueOf(1000);

System.out.println(integer.multiply(val)); // 1234000
final var integer = BigInteger.valueOf(1234);
final var val = BigInteger.valueOf(-2);

System.out.println(integer.multiply(val)); // -2468
final var integer = BigInteger.valueOf(Long.MAX_VALUE);
final var val = BigInteger.valueOf(Long.MIN_VALUE);

System.out.println(integer.multiply(val)); // -85070591730234615856620279821087277056

BigInteger negate ()

Returns a BigInteger whose value is (-this).

final var integer1 = BigInteger.valueOf(1234);
System.out.println(integer1); // 1234
System.out.println(integer1.negate()); // -1234

final var integer2 = BigInteger.valueOf(-5678);
System.out.println(integer2); // -5678
System.out.println(integer2.negate()); // 5678

System.out.println(BigInteger.ZERO.negate()); // 0

BigInteger nextProbablePrime ()

Returns the first integer greater than this BigInteger that is probably prime.

IntStream.range(0, 15).forEach(i -> {
    final var integer = BigInteger.valueOf(i);
    final var ret = integer.nextProbablePrime();
    System.out.println("integer = " + integer + " : ret = " + ret);
});

// Result
// ↓
//integer = 0 : ret = 2
//integer = 1 : ret = 2
//integer = 2 : ret = 3
//integer = 3 : ret = 5
//integer = 4 : ret = 5
//integer = 5 : ret = 7
//integer = 6 : ret = 7
//integer = 7 : ret = 11
//integer = 8 : ret = 11
//integer = 9 : ret = 11
//integer = 10 : ret = 11
//integer = 11 : ret = 13
//integer = 12 : ret = 13
//integer = 13 : ret = 17
//integer = 14 : ret = 17

BigInteger not ()

Returns a BigInteger whose value is (~this).

final var integer = new BigInteger(new byte[]{(byte) 0x0f, (byte) 0x0f});
final var ret = integer.not();

System.out.println(integer.toString(16)); // f0f
System.out.println(ret.toString(16)); // -f10

printBinary(integer); // 00001111 00001111
printBinary(ret);     // 11110000 11110000
final var integer = new BigInteger(new byte[]{(byte) 0xf0, (byte) 0xf0});
final var ret = integer.not();

System.out.println(integer.toString(16)); // -f10
System.out.println(ret.toString(16)); // f0f

printBinary(integer); // 11110000 11110000
printBinary(ret);     // 00001111 00001111

BigInteger or (BigInteger val)

Returns a BigInteger whose value is (this | val).

final var integer = new BigInteger(new byte[]{(byte) 0x0f});
final var val = new BigInteger(new byte[]{(byte) 0xaa});
final var ret = integer.or(val);

printBinary(integer); // 00001111
printBinary(val);     // 10101010
printBinary(ret);     // 10101111

BigInteger pow (int exponent)

Returns a BigInteger whose value is (thisexponent).

final var integer = BigInteger.valueOf(10);

System.out.println(integer.pow(0)); // 1
System.out.println(integer.pow(1)); // 10
System.out.println(integer.pow(2)); // 100
System.out.println(integer.pow(3)); // 1000

// ArithmeticException: Negative exponent
//integer.pow(-1);
final var integer = BigInteger.valueOf(-2);

System.out.println(integer.pow(0)); // 1
System.out.println(integer.pow(1)); // -2
System.out.println(integer.pow(2)); // 4
System.out.println(integer.pow(3)); // -8
System.out.println(integer.pow(4)); // 16

static BigInteger probablePrime (int bitLength, Random rnd)

Returns a positive BigInteger that is probably prime, with the specified bitLength.

final var rnd = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var integer = BigInteger.probablePrime(4, rnd);
    System.out.println(integer);
});

// Result
// ↓
//11
//11
//13
//13
//11
final var rnd = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var integer = BigInteger.probablePrime(8, rnd);
    System.out.println(integer);
});

// Result
// ↓
//157
//191
//193
//179
//191

BigInteger remainder (BigInteger val)

Returns a BigInteger whose value is (this % val).

Please see : mod(BigInteger m)

BigInteger setBit (int n)

Returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.

final var integer = new BigInteger(new byte[]{(byte) 0x00});
printBinary(integer); // 00000000

IntStream.range(0, 10).forEach(n -> {
    System.out.print("n = " + n + " : ");
    printBinary(integer.setBit(n));
});

// Result
// ↓
//n = 0 : 00000001
//n = 1 : 00000010
//n = 2 : 00000100
//n = 3 : 00001000
//n = 4 : 00010000
//n = 5 : 00100000
//n = 6 : 01000000
//n = 7 : 00000000 10000000
//n = 8 : 00000001 00000000
//n = 9 : 00000010 00000000

BigInteger shiftLeft (int n)

Returns a BigInteger whose value is (this << n).

final var integer = BigInteger.valueOf(1);

System.out.println(integer.shiftLeft(0)); // 1
System.out.println(integer.shiftLeft(1)); // 2
System.out.println(integer.shiftLeft(2)); // 4
System.out.println(integer.shiftLeft(3)); // 8
System.out.println(integer.shiftLeft(4)); // 16
System.out.println(integer.shiftLeft(5)); // 32
System.out.println(integer.shiftLeft(6)); // 64
System.out.println(integer.shiftLeft(7)); // 128
System.out.println(integer.shiftLeft(8)); // 256
final var integer = BigInteger.valueOf(-10);

System.out.println(integer.shiftLeft(0)); // -10
System.out.println(integer.shiftLeft(1)); // -20
System.out.println(integer.shiftLeft(2)); // -40
System.out.println(integer.shiftLeft(3)); // -80
System.out.println(integer.shiftLeft(4)); // -160
final var integer = BigInteger.valueOf(Long.MAX_VALUE);

System.out.println(integer.shiftLeft(0)); // 9223372036854775807
System.out.println(integer.shiftLeft(1)); // 18446744073709551614
System.out.println(integer.shiftLeft(2)); // 36893488147419103228

BigInteger shiftRight (int n)

Returns a BigInteger whose value is (this >> n).

final var integer = BigInteger.valueOf(64);

System.out.println(integer.shiftRight(0)); // 64
System.out.println(integer.shiftRight(1)); // 32
System.out.println(integer.shiftRight(2)); // 16
System.out.println(integer.shiftRight(3)); // 8
System.out.println(integer.shiftRight(4)); // 4
System.out.println(integer.shiftRight(5)); // 2
System.out.println(integer.shiftRight(6)); // 1
System.out.println(integer.shiftRight(7)); // 0
System.out.println(integer.shiftRight(8)); // 0
final var integer = BigInteger.valueOf(-32);

System.out.println(integer.shiftRight(0)); // -32
System.out.println(integer.shiftRight(1)); // -16
System.out.println(integer.shiftRight(2)); // -8
System.out.println(integer.shiftRight(3)); // -4
System.out.println(integer.shiftRight(4)); // -2
System.out.println(integer.shiftRight(5)); // -1
System.out.println(integer.shiftRight(6)); // -1

short shortValueExact ()

Converts this BigInteger to a short, checking for lost information.

System.out.println(BigInteger.ZERO.shortValueExact()); // 0
System.out.println(BigInteger.valueOf(1234).shortValueExact()); // 1234
System.out.println(BigInteger.valueOf(-5678).shortValueExact()); // -5678
System.out.println(Short.MAX_VALUE); // 32767
System.out.println(Short.MIN_VALUE); // -32768

System.out.println(new BigInteger("32767").shortValueExact()); // 32767
// ArithmeticException: BigInteger out of short range
//new BigInteger("32768").shortValueExact();

System.out.println(new BigInteger("-32768").shortValueExact()); // -32768
// ArithmeticException: BigInteger out of short range
//new BigInteger("-32769").shortValueExact();

int signum ()

Returns the signum function of this BigInteger.

System.out.println(BigInteger.valueOf(1234).signum()); // 1
System.out.println(BigInteger.valueOf(-5678).signum()); // -1
System.out.println(BigInteger.ZERO.signum()); // 0

BigInteger sqrt ()

Returns the integer square root of this BigInteger.

System.out.println(BigInteger.ONE.sqrt()); // 1
System.out.println(BigInteger.valueOf(9).sqrt()); // 3
System.out.println(BigInteger.valueOf(25).sqrt()); // 5
System.out.println(BigInteger.valueOf(100).sqrt()); // 10
System.out.println(Math.sqrt(5.0)); // 2.23606797749979
System.out.println(BigInteger.valueOf(5).sqrt()); // 2

System.out.println(Math.sqrt(14.0)); // 3.7416573867739413
System.out.println(BigInteger.valueOf(14).sqrt()); // 3

// ArithmeticException: Negative BigInteger
//BigInteger.valueOf(-1).sqrt();

BigInteger[] sqrtAndRemainder ()

Returns an array of two BigIntegers containing the integer square root s of this and its remainder this - s*s, respectively.

final var integer = BigInteger.valueOf(9);

// √9 = 3
final var ret = integer.sqrtAndRemainder();
System.out.println(ret.length); // 2
System.out.println(ret[0]); // 3
System.out.println(ret[1]); // 0
final var integer = BigInteger.valueOf(18);

// √18 = 4 (remainder of 2)
final var ret = integer.sqrtAndRemainder();
System.out.println(ret[0]); // 4
System.out.println(ret[1]); // 2

BigInteger subtract (BigInteger val)

Returns a BigInteger whose value is (this - val).

final var integer = new BigInteger("200");
final var val = new BigInteger("1000");

final var ret = integer.subtract(val);
System.out.println(ret); // -800
final var integer = new BigInteger("1000");
final var val = new BigInteger("-234");

final var ret = integer.subtract(val);
System.out.println(ret); // 1234
final var integer = new BigInteger("111111111122222222223333333333");
final var val = new BigInteger("111111111111111111111111111111");

final var ret = integer.subtract(val);
System.out.println(ret); // 11111111112222222222

boolean testBit (int n)

Returns true if and only if the designated bit is set.

final var integer = new BigInteger(new byte[]{(byte) 0x0f});
printBinary(integer); // 00001111 

IntStream.range(0, 8).forEach(n -> {
    final var ret = integer.testBit(n);
    System.out.println("n = " + n + " : ret = " + ret);
});

// Result
// ↓
//n = 0 : ret = true
//n = 1 : ret = true
//n = 2 : ret = true
//n = 3 : ret = true
//n = 4 : ret = false
//n = 5 : ret = false
//n = 6 : ret = false
//n = 7 : ret = false
final var integer = new BigInteger(new byte[]{(byte) 0xaa});
printBinary(integer); // 10101010

IntStream.range(0, 8).forEach(n -> {
    final var ret = integer.testBit(n);
    System.out.println("n = " + n + " : ret = " + ret);
});

// Result
// ↓
//n = 0 : ret = false
//n = 1 : ret = true
//n = 2 : ret = false
//n = 3 : ret = true
//n = 4 : ret = false
//n = 5 : ret = true
//n = 6 : ret = false
//n = 7 : ret = true

byte[] toByteArray ()

Returns a byte array containing the two's-complement representation of this BigInteger.

final var integer = new BigInteger(new byte[]{(byte) 0x7f, (byte) 0xff});
System.out.println(integer); // 32767

final var bytes = integer.toByteArray();
System.out.println(bytes.length); // 2
System.out.printf("%02x%n", bytes[0]); // 7f
System.out.printf("%02x%n", bytes[1]); // ff
final var integer = new BigInteger(new byte[]{(byte) 0xff, 0x00});
System.out.println(integer); // -256

final var bytes = integer.toByteArray();
System.out.println(bytes.length); // 2
System.out.printf("%02x%n", bytes[0]); // ff
System.out.printf("%02x%n", bytes[1]); // 00
final var integer = BigInteger.valueOf(0x123456789L);
System.out.println(integer); // 4886718345

final var bytes = integer.toByteArray();
System.out.println(bytes.length); // 5
System.out.printf("%02x%n", bytes[0]); // 01
System.out.printf("%02x%n", bytes[1]); // 23
System.out.printf("%02x%n", bytes[2]); // 45
System.out.printf("%02x%n", bytes[3]); // 67
System.out.printf("%02x%n", bytes[4]); // 89

String toString ()

Returns the decimal String representation of this BigInteger.

final var integer = BigInteger.valueOf(1234);
System.out.println(integer.toString()); // 1234
final var integer = BigInteger.valueOf(-4567);
System.out.println(integer.toString()); // -4567
final var integer = new BigInteger("ffff", 16);
System.out.println(integer.toString()); // 65535
final var integer = new BigInteger("1001", 2);
System.out.println(integer.toString()); // 9

String toString (int radix)

Returns the String representation of this BigInteger in the given radix.

final var integer = BigInteger.valueOf(255);
System.out.println(integer); // 255
System.out.println(integer.toString(16)); // ff
System.out.println(integer.toString(2)); // 11111111
final var integer = new BigInteger("-abcd", 16);
System.out.println(integer); // -43981
System.out.println(integer.toString(16)); // -abcd
System.out.println(integer.toString(2)); // -1010101111001101
final var integer = new BigInteger("11111111000000001111111100000000", 2);
System.out.println(integer); // 4278255360
System.out.println(integer.toString(16)); // ff00ff00
System.out.println(integer.toString(2)); // 11111111000000001111111100000000

static BigInteger valueOf (long val)

Returns a BigInteger whose value is equal to that of the specified long.

System.out.println(BigInteger.valueOf(1234)); // 1234
System.out.println(BigInteger.valueOf(-5678)); // -5678

System.out.println(BigInteger.valueOf(Long.MAX_VALUE)); // 9223372036854775807
System.out.println(BigInteger.valueOf(Long.MIN_VALUE)); // -9223372036854775808

BigInteger xor (BigInteger val)

Returns a BigInteger whose value is (this ^ val).

final var integer = new BigInteger(new byte[]{(byte) 0x0f});
final var val = new BigInteger(new byte[]{(byte) 0xaa});
final var ret = integer.xor(val);

printBinary(integer); // 00001111
printBinary(val);     // 10101010
printBinary(ret);     // 10100101

Methods declared in Number

byteValue, shortValue

Please see the link below.


Related posts

To top of page