import java.math.BigInteger;
BigInteger 对象的创建
BigInteger a = new BigInteger("123");
String str = "123";
BigInteger a = BigInteger.valueOf(str);
int num = 456;
BigInteger a = BigInteger.valueOf(num);
基本常量
a = BigInteger.ONE
b = BigInteger.TEN
c = BigInteger.ZERO
BigInteger 的输入输出
直接读入 BigInteger
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
BigInteger a;
a = in.nextBigInteger();
System.out.print(a.toString());
}
间接读入 BigInteger
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
String s = in.nextLine();
BigInteger a = new BigInteger(s);
System.out.print(a.toString());
}
BigInteger 直接输出
System.out.print(a);
BigInteger 转化成十进制表示的 String
System.out.print(a.toString());
BigInteger 转化成 p 进制表示的 String
int p = 2;
System.out.print(a.toString(p));
BigInteger 二进制下的长度
BigInteger n = new BigInteger("12");
System.out.println(n.bitLength());
BigInteger 之间的比较
BigInteger a = new BigInteger("123");
BigInteger b = new BigInteger("456");
System.out.println(a.equals(b));
if(a.compareTo(b) == 0) System.out.println("a == b");
else if(a.compareTo(b) > 0) System.out.println("a > b");
else if(a.compareTo(b) < 0) System.out.println("a < b");
基本运算
比较
a.compareTo(b) == 0
a.compareTo(b) > 0
a.compareTo(b) < 0
加法
a.add(b)
减法
a.subtract(b)
乘法
a.multiply(b)
除法
a.devide(b)
取余
a.remainder(b)
绝对值
a.abs()
取反
a.negate()
幂
a.pow(3)
intValue()
longValue()将此BigInteger转换为long。
sqrt() 返回此BigInteger的整数平方根。