不變的任意精度的整數(shù)。 在所有的操作中
BigIntegers 看起來好象是用二進(jìn)制補(bǔ)碼(類似 Java
的基本整數(shù)類型)表示的 。BigIntegers 提供類似于所有 Java
基本整數(shù)操作符和來自 java.lang.Math 的所有相關(guān)靜態(tài)方法。 此外,
BigIntegers 還提供模算術(shù)操作, GCD 計(jì)算,基本測(cè)試,初期生成,位處理和一些其它的零碎操作。
如同《Java 語言規(guī)范》中的定義,算術(shù)運(yùn)算的語義極為類似于 java 的整數(shù)算術(shù)運(yùn)算符。
例如,被零除將拋出 ArithmeticException ,一個(gè)負(fù)數(shù)除以一個(gè)正數(shù)將產(chǎn)生一個(gè)負(fù)
(或零)余數(shù)。
《規(guī)范》中所有關(guān)于溢出的細(xì)節(jié)都忽略了,因?yàn)?BigIntegers
能夠保存任意大數(shù)值的操作結(jié)果。
移位操作的語義擴(kuò)展了 Java 中的移位運(yùn)算符而允許負(fù)的移位距離。
移動(dòng)負(fù)距離的右移的結(jié)果是一個(gè)左移,反之類似。無符號(hào)的右移運(yùn)算符
(>>>) 被忽略,因?yàn)樵摬僮鲗?duì)該類提供的混合了“無限字長”的抽象沒有多大意義。
位邏輯運(yùn)算語義完全模擬
Java 的位整數(shù)運(yùn)算符。二目運(yùn)算符 (與,或,異或)
在執(zhí)行操作之前隱含地對(duì)兩個(gè)操作數(shù)中的較短者執(zhí)行符號(hào)擴(kuò)展。
比較操作執(zhí)行帶正負(fù)號(hào)的整數(shù)比較,類似于
java 的關(guān)系比較和相等運(yùn)算符所作的比較。
提供模算術(shù)運(yùn)算操作來計(jì)算余數(shù)、
乘冪和多重逆。這些方法總是返回一個(gè)在 0 和 (模數(shù) - 1) 之間的,包括邊界在內(nèi)的)非負(fù)結(jié)果。
位操作對(duì)它們操作數(shù)的二進(jìn)制補(bǔ)碼的位進(jìn)行操作。
如果必要的話,操作數(shù)要做擴(kuò)展以便它包含運(yùn)算所需的位。
沒有任何位操作能產(chǎn)生一個(gè)同所要操作的
BigInteger 符號(hào)不同的數(shù),因?yàn)樗鼈儍H能影響單個(gè)位,并且該類提供的 “無限字長”抽象保證了在每個(gè)
BigInteger 之前有無限多個(gè) “虛擬符號(hào)位”。
類 java.math.BigInteger
java.lang.Object
|
+----java.lang.Number
|
+----java.math.BigInteger
- public class BigInteger
- extends Number
- 參見:
- BigDecimal
構(gòu)造子索引
- BigInteger(byte[])
- 把一個(gè)包含著(帶正負(fù)號(hào))整數(shù)的二進(jìn)制補(bǔ)碼的字節(jié)數(shù)組翻譯為 BigInteger 。
- BigInteger(int, byte[])
- 把一個(gè)整數(shù)的 sign-magnitude 表示法翻譯為 BigInteger 。
- BigInteger(int, int, Random)
- 返回有指定 bitLength(可能是素?cái)?shù))的隨機(jī)選擇的 BigInter 。
- BigInteger(int, Random)
- 返回一個(gè)隨機(jī)數(shù),均勻分布在 [0, 2**numBits - 1] 之間
(假設(shè)由 rndSrc 提供一個(gè)公平的隨機(jī)源) 。
- BigInteger(String)
- 把一個(gè)字符串翻譯為一個(gè) BigInteger ,該字符串包含可選的負(fù)號(hào),后面跟著一個(gè)或多個(gè)十進(jìn)制數(shù)字序列。
- BigInteger(String, int)
- 把一個(gè)字符串翻譯為一個(gè) BigInteger ,該字符串包含可選的負(fù)號(hào),后面跟著一個(gè)或多個(gè)指定進(jìn)制的數(shù)字序列。
方法索引
- abs()
- 返回一個(gè) BigInteger ,它等于該數(shù)字的絕對(duì)值。
- add(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this + val) 。
- and(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this & val) 。
- andNot(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this & ~val) 。
- bitCount()
- 返回該數(shù)的二進(jìn)制補(bǔ)碼表示中不包括符號(hào)位在內(nèi)的位的個(gè)數(shù)。
- bitLength()
- 返回該數(shù)的最小的二進(jìn)制補(bǔ)碼表示法中位的個(gè)數(shù),
即 *不包括* 一個(gè)符號(hào)位,例如(ceil(log2(this <0 ? -this : this + 1))). - clearBit(int)
- 返回一個(gè) BigInteger ,它等于該數(shù)指定位清零后所得的值。
- compareTo(BigInteger)
- 根據(jù)該數(shù)字是小于、等于還是大于 val 返回 -1、0 或 1。
- divide(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this / val) 。
- divideAndRemainder(BigInteger)
- 返回一個(gè)包含兩個(gè) BigInteger 的數(shù)組。
- doubleValue()
- 把該數(shù)字轉(zhuǎn)換為一個(gè) double 型。
- equals(Object)
- 如果 x 是等于該數(shù)則返回 true 。
- flipBit(int)
- 返回一個(gè) BigInteger ,它等于該數(shù)指定位取反后所得的值。
- floatValue()
- 把該數(shù)字轉(zhuǎn)換為一個(gè) float 型。
- gcd(BigInteger)
- 返回值為 abs(this) 和 abs(val) 最大公分母的 BigInteger 。
- getLowestSetBit()
- 返回該數(shù)最右端 (lowest-order)是 1 的位的索引值,
(即距最右 1 位之前的 0 位的個(gè)數(shù) )。
- hashCode()
- 為該對(duì)象計(jì)算一個(gè)散列碼。
- intValue()
- 把該數(shù)字轉(zhuǎn)換為一個(gè) int 值。
- isProbablePrime(int)
- 如果該 BigInteger 是素?cái)?shù),則返回 true ;如果該 BigInteger 是合數(shù)則返回 false 。
- longValue()
- 把該數(shù)字轉(zhuǎn)換為一個(gè) long 型值。
- max(BigInteger)
- 返回 BigInteger ,其值是 this 和 val 中的較大者。
- min(BigInteger)
- 返回 BigInteger ,其值是 this 和 val 中的較小者。
- mod(BigInteger)
- 返回一個(gè) BigInteger ,其值是(this mod m )。
- modInverse(BigInteger)
- 返回 this 取模 m 的模多重逆。
- modPow(BigInteger, BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this ** exponent) mod m 。
- multiply(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this * val) 。
- negate()
- 返回一個(gè) BigInteger ,其值是 (-1 * this ) 。
- not()
- 返回一個(gè) BigInteger ,其值是 (~this) 。
- or(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this | val) 。
- pow(int)
- 返回一個(gè) BigInteger ,其值是 (this ** exponent) 。
- remainder(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this % val) 。
- setBit(int)
- 返回一個(gè) BigInteger ,其值等于該數(shù)被設(shè)置指定位后所得的值。
- shiftLeft(int)
- 返回一個(gè) BigInteger ,其值是 (this << n).
- shiftRight(int)
- 返回一個(gè) BigInteger ,其值是 (this >> n).
- signum()
- 返回該數(shù)值的符號(hào)(即根據(jù)該數(shù)的值是正、零或負(fù)返回 -1 、0 或 1)。
- subtract(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this - val)。
- testBit(int)
- 如果設(shè)置了指定位則返回 true 。
- toByteArray()
- 返回該數(shù)值的二進(jìn)制補(bǔ)碼表示。
- toString()
- 以 10 為基數(shù)返回表示該數(shù)的字符串。
- toString(int)
- 以指定數(shù)為基數(shù)返回表示該數(shù)的字符串。
- valueOf(long)
- 返回一個(gè)是指定值的 BigInteger 。
- xor(BigInteger)
- 返回一個(gè) BigInteger ,其值是 (this ^ val) 。
構(gòu)造子
BigInteger
public BigInteger(byte val[]) throws NumberFormatException
- 把一個(gè)(帶符號(hào))整數(shù)的二進(jìn)制補(bǔ)碼表示的字節(jié)數(shù)組翻譯為
BigInteger 。假定輸入數(shù)組是 big-endian 格式(即最有效字節(jié)在位置[0])
(最有效字節(jié)的最有效位是符號(hào)位)。數(shù)組必須至少包含一個(gè)字節(jié),否則將拋出 NumberFormatException 。
BigInteger
public BigInteger(int signum,
byte magnitude[]) throws NumberFormatException
- 把一個(gè)整數(shù)的 sign-magnitude 表示法翻譯為 BigInteger 。符號(hào)表示為一個(gè)整數(shù)數(shù)值 (-1 表示負(fù)數(shù)、0 表示零、1 表示正數(shù))。數(shù)量用
big-endian
結(jié)構(gòu)表示(即最有效字節(jié)在位置[0])。一個(gè)無效的符號(hào)數(shù)值或一個(gè) 0 符號(hào)數(shù)值伴隨一個(gè)非零數(shù)量將導(dǎo)致 NumberFormatException 。零長度大小的數(shù)組是允許的,并且將得到結(jié)果
0 (不管給定的符號(hào)數(shù)值是什么) 。
BigInteger
public BigInteger(String val,
int radix) throws NumberFormatException
- 把一個(gè)字符串翻譯為 BigInteger ,該字符串包含可選的負(fù)號(hào),后面跟著一個(gè)或多個(gè)指定進(jìn)制的數(shù)字序列
(字符到數(shù)字的映射是由 Character.digit 提供的)。一個(gè)外部字符 (包括空白符),或一個(gè)超出從
Character.MIN_RADIX(2) 到 Character.MAX_RADIX(36) 范圍的基數(shù)(
包括邊界),將導(dǎo)致 NumberFormatException 異常。
BigInteger
public BigInteger(String val) throws NumberFormatException
- 把一個(gè)字符串翻譯為 BigInteger ,該字符串包含可選的負(fù)號(hào),后面跟著一個(gè)或多個(gè)十進(jìn)制數(shù)字序列。字符到數(shù)字的映射是由 Character.digit 提供的。任何外部字符 (包括空白符) 將導(dǎo)致 NumberFormatException 異常。
BigInteger
public BigInteger(int numBits,
Random rndSrc) throws IllegalArgumentException
- 返回一個(gè)隨機(jī)數(shù),均勻分布在 [0, 2**numBits - 1] 之間
(假設(shè)由 rndSrc 提供一個(gè)公平的隨機(jī)源)。
注意該構(gòu)造子總是返回一個(gè)非負(fù)的 BigInteger 。
如果 numBits <0,則拋出 illegalargumentexception 。
BigInteger
public BigInteger(int bitLength,
int certainty,
Random rnd)
- 返回一個(gè)指定 bitLength (可能是一個(gè)素?cái)?shù))的隨機(jī)選擇的 BigInter 。參數(shù) certainty 是一個(gè)對(duì)調(diào)用者愿意忍受的不確定性的度量:該數(shù)是素?cái)?shù)的概率將超過 1 - 1/2**certainty 。執(zhí)行時(shí)間正比于確定性參數(shù)的值。用給定的隨機(jī)數(shù)發(fā)生器選擇進(jìn)行素?cái)?shù)測(cè)試的候選數(shù)。如果 bitLength <2 ,則拋出 arithmeticexception。
方法
valueOf
public static BigInteger valueOf(long val)
- 返回一個(gè)是指定值的 BigInteger 。 給該工廠提供首選的
(long) 構(gòu)造子是因?yàn)樗试S對(duì)頻繁使用的 BigIntegers (如 0 和 1)的重用,這種操作不需要輸出常數(shù)。
add
public BigInteger add(BigInteger val) throws ArithmeticException
- 返回一個(gè) BigInteger ,其值是 (this + val) 。
subtract
public BigInteger subtract(BigInteger val)
- 返回一個(gè) BigInteger ,其值是 (this - val) 。
multiply
public BigInteger multiply(BigInteger val)
- 返回一個(gè) BigInteger ,其值是 (this * val) 。
divide
public BigInteger divide(BigInteger val) throws ArithmeticException
- 返回一個(gè) BigInteger ,其值是 (this / val) 。 如果 val == 0 ,則拋出 ArithmeticException 。
remainder
public BigInteger remainder(BigInteger val) throws ArithmeticException
- 返回一個(gè) BigInteger ,其值是 (this % val) 。 如果 val == 0,則拋出 ArithmeticException 。
divideAndRemainder
public BigInteger[] divideAndRemainder(BigInteger val) throws ArithmeticException
- 返回一個(gè)包含兩個(gè) BigIntegers 的數(shù)組。 返回值的第一個(gè) ([0]) 元素是商
(this / val), 第二個(gè) ([1])
元素是余數(shù) (this % val) 。如果 val == 0 ,則拋出 ArithmeticException 。
pow
public BigInteger pow(int exponent) throws ArithmeticException
- 返回一個(gè) BigInteger ,其值是 (this ** exponent) 。 如果
exponent <0(因?yàn)樵摬僮鲗a(chǎn)生一個(gè)非整型值),則拋出 arithmeticexception 。注意:指數(shù)是一個(gè)整型數(shù)而不是 biginteger 。
gcd
public BigInteger gcd(BigInteger val)
- 返回值為 abs(this) 和 abs(val) 最大公分母的 BigInteger 。 如果 this == 0 && val == 0,則返回 0。
abs
public BigInteger abs()
- 返回一個(gè) BigInteger ,它是該數(shù)值的絕對(duì)值。
negate
public BigInteger negate()
- 返回一個(gè) BigInteger ,其值是 (-1 * this ) 。
signum
public int signum()
- 返回該數(shù)值的符號(hào)數(shù) (根據(jù)該數(shù)的值是正、零或負(fù)返回 -1 、
0 或 1 ) 。
mod
public BigInteger mod(BigInteger m)
- 返回一個(gè) BigInteger ,其值是 this mod m 。 如果 m <= 0,則拋出 arithmeticexception。
modPow
public BigInteger modPow(BigInteger exponent,
BigInteger m)
- 返回一個(gè) BigInteger ,其值是 (this ** exponent) mod m 。 如果
exponent == 1, 返回值是 (this mod m) 。 如果 exponent <0 , 返回值是 (this ** -exponent)的模多重逆。如果 m <="0,則拋出" arithmeticexception 。
modInverse
public BigInteger modInverse(BigInteger m) throws ArithmeticException
- 返回 this 取模 m 的模多重逆。 如果 m<= 0 或 this 沒有多重逆 mod m (比如 gcd(this, m) !="1),則拋出" arithmeticexception 。
shiftLeft
public BigInteger shiftLeft(int n)
- 返回一個(gè) BigInteger ,其值是 (this << n)。計(jì)算 floor(this * 2**n)。
shiftRight
public BigInteger shiftRight(int n)
- 返回一個(gè) BigInteger ,其值是 (this >> n)。 執(zhí)行符號(hào)擴(kuò)展(計(jì)算
floor(this / 2**n))。
and
public BigInteger and(BigInteger val)
- 返回一個(gè) BigInteger ,其值是 (this & val) 。
(如果 this 和 val 二者都是負(fù)的,則該方法返回一個(gè)負(fù)數(shù)。)
or
public BigInteger or(BigInteger val)
- 返回一個(gè) BigInteger ,其值是 (this | val) 。
(如果 this 和 val 二者之一是負(fù)的,則該方法返回一個(gè)負(fù)數(shù)。)
xor
public BigInteger xor(BigInteger val)
- 返回一個(gè) BigInteger ,其值是 (this ^ val) 。
(如果 this 和 val 二者只有一個(gè)是負(fù)的,則該方法返回一個(gè)負(fù)數(shù)。)
not
public BigInteger not()
- 返回一個(gè) BigInteger ,其值是 (~this) 。
(如果 this 是非負(fù)的,則該方法返回一個(gè)負(fù)數(shù)。)
andNot
public BigInteger andNot(BigInteger val)
- 返回一個(gè) BigInteger ,其值是 (this & ~val) 。 該方法等價(jià)于 and(val.not()),它是進(jìn)行掩模操作的便捷方法。
(如果 this 是負(fù)數(shù)并且 val 是正數(shù),則該方法返回一個(gè)負(fù)數(shù)。)
testBit
public boolean testBit(int n) throws ArithmeticException
- 如果設(shè)置了指定位則返回 true 。 (計(jì)算
((this & (1 << n)) !="0))。如果 n < 0,則拋出 arithmeticexception。
setBit
public BigInteger setBit(int n) throws ArithmeticException
- 返回一個(gè) BigInteger ,其值等于該數(shù)被設(shè)置指定位后所得值(計(jì)算 (this | (1
<< n)))。 如果 n < 0 ,則拋出 arithmeticexception 。
clearBit
public BigInteger clearBit(int n) throws ArithmeticException
- 返回一個(gè) BigInteger ,其值等于該數(shù)指定位清零后所得值(計(jì)算 (this & ~(1
<< n)))。 如果 n < 0,則拋出 arithmeticexception 。
flipBit
public BigInteger flipBit(int n) throws ArithmeticException
- 返回一個(gè) BigInteger ,其值等于該數(shù)指定位取反后所得值(計(jì)算 (this ^ (1
<< n)))。 如果 n < 0,則拋出 arithmeticexception 。
getLowestSetBit
public int getLowestSetBit()
- 返回該數(shù)最右端 (lowest-order)是 1 的位的索引
(即就是距最右端 1 位間的 0 位的個(gè)數(shù) ) 。 如果該數(shù)沒有 1 位,則返回 -1
(計(jì)算 (this==0? -1 : log2(this & -this)))。
bitLength
public int bitLength()
- 返回該數(shù)的最小二進(jìn)制補(bǔ)碼表示的位的個(gè)數(shù),
即 *不包括* 符號(hào)位
(ceil(log2(this <0 ? -this : this + 1)))。對(duì)正數(shù)來說,這等價(jià)于普通二進(jìn)制表示的位的個(gè)數(shù)。
bitCount
public int bitCount()
- 返回該數(shù)的二進(jìn)制補(bǔ)碼表示中不包擴(kuò)符號(hào)位在內(nèi)的位的個(gè)數(shù)。
該方法在 BigIntegers 之上實(shí)現(xiàn)位向量風(fēng)格的集合時(shí)很有用。
isProbablePrime
public boolean isProbablePrime(int certainty)
- 如果該 BigInteger 可能是素?cái)?shù),則返回 true ;如果它很明確是一個(gè)合數(shù),則返回 false 。
參數(shù) certainty 是對(duì)調(diào)用者愿意忍受的不確定性的度量:如果該數(shù)是素?cái)?shù)的概率超過了 1 - 1/2**certainty方法,則該方法返回
true 。執(zhí)行時(shí)間正比于參數(shù)確定性的值。
compareTo
public int compareTo(BigInteger val)
- 根據(jù)該數(shù)值是小于、等于、或大于 val 返回 -1、0 或 1 。該方法在六個(gè)邏輯比較運(yùn)算符
(<, ="=,">, >=, !=, <= ) 的操作中作為首選方法。進(jìn)行這些比較的建議方法是:(x.compareto(y) 0) ,其中 是六個(gè)比較符中的一個(gè)。
equals
public boolean equals(Object x)
- 如果 x 是一個(gè) BigInteger 并且等于該數(shù)則返回 true 。
提供該方法的目的是使 BigIntegers 可以作為散列碼關(guān)鍵字使用。
- 覆蓋:
- 類 Object 中的 equals
min
public BigInteger min(BigInteger val)
- 返回 BigInteger ,其值是 this 和 val 中的較小者。
若值相同,則兩者都可能被返回。
max
public BigInteger max(BigInteger val)
- 返回 BigInteger ,其值是 this 和 val 中的較大者。
若值相同,則兩者都可能被返回。
hashCode
public int hashCode()
- 為該對(duì)象計(jì)算一個(gè)散列碼。
- 覆蓋:
- 類 Object 中的 hashCode
toString
public String toString(int radix)
- 返回表示該數(shù)的字符串,基數(shù)為給定基數(shù) 。
如果基數(shù)超出了
Character.MIN_RADIX(2) 到 Character.MAX_RADIX(36)(包括兩者在內(nèi))
的范圍,它會(huì)缺省設(shè)定為 10 (類似于 Integer.toString 的情形)。使用由 Character.forDigit 提供的數(shù)字到字符的映射,并且如果適當(dāng)?shù)脑?,還可以前置一個(gè)負(fù)號(hào)。 該表示法同 (String, int) 構(gòu)造子兼容。
toString
public String toString()
- 返回表示該數(shù)的字符串,基數(shù)為 10 。
使用由 Character.forDigit 提供的數(shù)字到字符的映射,并且如果合適的話,還可以前置一個(gè)負(fù)號(hào)。
該表示法同 (String) 構(gòu)造子兼容,并且允許用 Java 的 + 運(yùn)算符做字符串連接操作。
- 覆蓋:
- 類 Object 中的
toString
toByteArray
public byte[] toByteArray()
- 返回該數(shù)值的二進(jìn)制補(bǔ)碼表示。 數(shù)組是 big-endian 格式(即最有效字節(jié)在位置 [0]) 。該數(shù)組包含了需要表示該數(shù)的最小的字節(jié)數(shù)
(ceil((this.bitLength() + 1)/8)) 。該表示法同(byte[])構(gòu)造子兼容。
intValue
public int intValue()
- 把該數(shù)轉(zhuǎn)換為 int 值。 標(biāo)準(zhǔn)的限制原語轉(zhuǎn)換同《Java 語言規(guī)范》所定義的一樣。
- 覆蓋:
- 類 Number 中的
intValue
longValue
public long longValue()
- 把該數(shù)轉(zhuǎn)換為 long 型。標(biāo)準(zhǔn)的限制原語轉(zhuǎn)換同《Java 語言規(guī)范》定義的一樣。
- 覆蓋:
- 類 Number 中的
longValue
floatValue
public float floatValue()
- 把該數(shù)轉(zhuǎn)換為 float 型。該操作類似于《Java 語言規(guī)范》中定義的
double-to-float 限制原語轉(zhuǎn)換:如果數(shù)值太大以致不能表示為一個(gè)浮點(diǎn)數(shù)時(shí),它將被轉(zhuǎn)換為合適的無窮大或負(fù)無窮大。
- 覆蓋:
- 類
Number 中的
floatValue
doubleValue
public double doubleValue()
- 把該數(shù)轉(zhuǎn)換為 double 型。 該操作類似于《Java 語言規(guī)范》中定義的
double-to-float 的限制原語轉(zhuǎn)換:如果數(shù)值太大以致不能表示為一個(gè)雙精度數(shù),它將被轉(zhuǎn)換為適當(dāng)?shù)臒o窮大或負(fù)無窮大。
- 覆蓋:
- 類
Number 中的
doubleValue