Constructor
BigInteger(byte[] val)
- Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.
BigInteger(int signum, byte[] magnitude)
- Translates the sign-magnitude representation of a BigInteger into a BigInteger.
BigInteger(int bitLength, int certainty, Random rnd)
- Creates a randomly generated positive BigInteger that is probably prime, with the specified bitLength.
BigInteger(int numBits, Random rnd)
- Creates a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive.
BigInteger(String val)
- Translates the decimal String representation of a BigInteger into a BigInteger.
BigInteger(String val, int radix)
- Translates the String representation of a BigInteger in the specified radix into a BigInteger.
The following code use
new BigInteger(byte[] val)
to create a BigInteger.
import java.math.BigInteger;
public class Main {
public static void main(String[] argv) throws Exception {
// A negative value
byte[] bytes = new byte[] { (byte) 0xFF, 0x00, 0x00 }; // -65536
// A positive value
bytes = new byte[] { 0x1, 0x00, 0x00 }; // 65536
BigInteger bi = new BigInteger(bytes);
}
}
The following code use
new BigInteger(String val)
to create a BigInteger.
import java.math.BigInteger;
/*
Here's Long.MAX_VALUE: 9223372036854775807
Here's a bigger number: 3419229223372036854775807
Here it is as a double: 3.419229223372037E24
*/
public class MainClass {
public static void main(String[] args) {
System.out.println("Here's Long.MAX_VALUE: " + Long.MAX_VALUE);
BigInteger bInt = new BigInteger("3419229223372036854775807");
System.out.println("Here's a bigger number: " + bInt);
System.out.println("Here it is as a double: " + bInt.doubleValue());
}
}
You can set the radix for the string value passed in with
new BigInteger(String val, int radix)
import java.math.BigInteger;
public class Main {
public static void main(String[] argv) throws Exception {
BigInteger bi = new BigInteger("4407760", 8);
}
}
new BigInteger(int bitLength, int certainty, Random rnd)
creates a BigInteger with a random number
import java.math.BigInteger;
import java.security.SecureRandom;
public class Main {
public static void main(String[] args) throws Exception {
int bitLength = 512; // 512 bits
SecureRandom rnd = new SecureRandom();
int certainty = 90; // 1 - 1/2(90) certainty
System.out.println("BitLength : " + bitLength);
BigInteger mod = new BigInteger(bitLength, certainty, rnd);
BigInteger exponent = BigInteger.probablePrime(bitLength, rnd);
BigInteger n = BigInteger.probablePrime(bitLength, rnd);
BigInteger result = n.modPow(exponent, mod);
System.out.println("Number ^ Exponent MOD Modulus = Result");
System.out.println("Number");
System.out.println(n);
System.out.println("Exponent");
System.out.println(exponent);
System.out.println("Modulus");
System.out.println(mod);
System.out.println("Result");
System.out.println(result);
}
}
Home
Java Book
Essential Classes
Java Book
Essential Classes
BigInteger:
- BigInteger class
- BigInteger.TEN
- BigInteger.ZERO
- Constructor
- BigInteger: add(BigInteger val)
- BigInteger: andNot(BigInteger val)
- BigInteger: bitLength()
- BigInteger: clearBit(int n)
- BigInteger: divide(BigInteger val)
- BigInteger: doubleValue()
- BigInteger: flipBit(int n)
- BigInteger: isProbablePrime(int certainty)
- BigInteger: modPow(BigInteger exponent, BigInteger m)
- BigInteger: multiply(BigInteger val)
- BigInteger: negate()
- BigInteger: not()
- BigInteger: or(BigInteger val)
- BigInteger: probablePrime(int bitLength, Random rnd)
- BigInteger: pow(int exponent)
- BigInteger: setBit(int n)
- BigInteger: shiftLeft(int n)
- BigInteger: shiftRight(int n)
- BigInteger: subtract(BigInteger val)
- BigInteger: testBit(int n)
- BigInteger: toByteArray()
- BigInteger: toString()
- BigInteger: toString(int radix)
- BigInteger: valueOf(long val)
- BigInteger: xor(BigInteger val)