Java 中的 BigInteger 类

BigInteger 类用于执行涉及非常大整数计算的数学运算,这些计算超出了所有可用基本数据类型的限制。

由于拥有丰富的方法库,BigInteger 类使用起来非常方便,并且在竞赛编程中也被广泛使用。下面让我们列出了基本数据类型的简单算术语句与 BigInteger 对象的对应语句。

示例:

int a, b;                
BigInteger A, B;

初始化方式如下:

a = 54;
b = 23;
A  = BigInteger.valueOf(54);
B  = BigInteger.valueOf(37);

对于字符串形式的整数,我们可以这样初始化:

A  = new BigInteger(“54”);
B  = new BigInteger(“123456789123456789”);

为了方便初始化,BigInteger 类中还定义了一些常量,如下所示:

A = BigInteger.ONE;
// 除了这个,可用的常量还有 BigInteger.ZERO 
// 和 BigInteger.TEN

数学运算如下所示:

int c = a + b;
BigInteger C = A.add(B);

其他类似的函数还有 subtract()、multiply()、divide()、remainder(),但所有这些函数都接受 BigInteger 作为参数。所以,如果我们想对整数或字符串进行这些操作,必须先将它们转换为 BigInteger,然后再传递给函数,如下所示:

String str = “123456789”;
BigInteger C = A.add(new BigInteger(str));
int val  = 123456789;
BigInteger C = A.add(BigInteger.valueOf(val));

从 BigInteger 中提取值的方法如下:

int x   =  A.intValue();   // 值应在 int x 的范围内
long y   = A.longValue();  // 值应在 long y 的范围内
String z = A.toString();

比较操作:

if (a < b) {}         // 对于基本 int 类型
if (A.compareTo(B) < 0)  {} // 对于 BigInteger

实际上,compareTo 会根据数值大小返回 -1(小于)、0(等于)或 1(大于)。对于相等性判断,我们也可以使用:

if (A.equals(B)) {}  // A 等于 B

BigInteger 类的方法

方法

执行的操作

add(BigInteger val)

返回一个值为 (this + val) 的 BigInteger。

abs()

返回一个值为该 BigInteger 绝对值的 BigInteger。

and(BigInteger val)

返回一个值为 (this & val) 的 BigInteger。

andNot(BigInteger val)

返回一个值为 (this & ~val) 的 BigInteger。

bitCount()

返回该 BigInteger 的二进制补码表示中,与其符号位不同的位数。

bitLength()

返回该 BigInteger 的最小二进制补码表示中的位数,不包括符号位。

byteValueExact()

将此 BigInteger 转换为 byte,并检查信息丢失情况。

clearBit(int n)

返回一个 BigInteger,其值等效于将指定位清零后的此 BigInteger。

compareTo(BigInteger val)

将此 BigInteger 与指定的 BigInteger 进行比较。

divide(BigInteger val)

返回一个值为 (this / val) 的 BigInteger。

divideAndRemainder(BigInteger val)

返回一个包含两个 BigInteger 的数组,依次为 (this / val) 和 (this % val)。

doubleValue()

将此 BigInteger 转换为 double。

equals(Object x)

将此 BigInteger 与指定对象进行相等性比较。

flipBit(int n)

返回一个 BigInteger,其值等效于将指定位翻转后的此 BigInteger。

floatValue()

将此 BigInteger 转换为 float。

gcd(BigInteger val)

返回一个值为 abs(this) 和 abs(val) 最大公约数的 BigInteger。

getLowestSetBit()

返回此 BigInteger 中最右边(最低位)1 位的索引(即最右边 1 位右侧的 0 位个数)。

hashCode()

返回此 BigInteger 的哈希码。

intValue()

将此 BigInteger 转换为 int。

intValueExact()

将此 BigInteger 转换为 int,并检查信息丢失情况。

isProbablePrime(int certainty)

如果此 BigInteger 可能为素数,则返回 true;如果它肯定为合数,则返回 false。

longValue()

将此 BigInteger 转换为 long。

longValueExact()

将此 BigInteger 转换为 long,并检查信息丢失情况。

max(BigInteger val)

返回此 BigInteger 和 val 中的最大值。

min(BigInteger val)

返回此 BigInteger 和 val 中的最小值。

mod(BigInteger m)

返回一个值为 (this mod m) 的 BigInteger。

modInverse(BigInteger m)

返回一个值为 (this-1 mod m) 的 BigInteger。

modPow(BigInteger exponent, BigInteger m)

返回一个值为 (thisexponent mod m) 的 BigInteger。

multiply(BigInteger val)

返回一个值为 (this * val) 的 BigInteger。

negate()

返回一个值为 (-this) 的 BigInteger。

nextProbablePrime()

返回大于此 BigInteger 的第一个可能为素数的整数。

not()

返回一个值为 (~this) 的 BigInteger。

or(BigInteger val)

返回一个值为 (this

val) 的 BigInteger。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/17543.html
点赞
0.00 平均评分 (0% 分数) - 0