位置:java.math.BigInteger
作用:提供高精度整型數據類型及相關操作
一、基本介紹
- BigInteger為不可變的任意精度的整數(對象創建后無法改變,每次運算均會產生一個新的對象)。
- 所有操作中,都以二進制補碼形式表示 BigInteger(同Java 的基本整數類型)。
- 提供所有 Java 的基本整數操作符(* / + -等等)的對應物(一堆函數)。
- 提供 java.lang.Math 的所有相關方法,此外還提供模算術、GCD 計算(最大公約數)、質數測試、素數生成、位操作以及一些其他操作。
- 算術運算、逐位邏輯運算的語義完全模仿 Java 整數算術運算符的語義。
- 位移操作允許產生負位移距離,且忽略了無符號的右位移運算符(>>>)。
- 比較操作執行有符號的整數比較,類似於 Java 的關系運算符和相等性運算符執行的比較。
- 模算術操作用來計算余數、求冪和乘法可逆元。這些方法始終返回非負結果,范圍在 0 和 (modulus - 1)(包括)之間。
- 位操作對其操作數的二進制補碼表示形式的單個位進行操作。如有必要,操作數會通過擴展符號來包含指定的位。單一位操作不能產生與正在被操作的 BigInteger 符號不同的 BigInteger,因為它們僅僅影響單個位,並且此類提供的“無窮大詞大小”抽象可保證在每個 BigInteger 前存在無窮多的“虛擬符號位”數。
- 當為任何輸入參數傳遞 null 對象引用時,此類中的所有方法和構造方法都將拋出
NullPointerException
。
二、字段
- static BigInteger ONE //BigInteger常量1
- static BigInteger TEN //BigInteger常量10
- static BigInteger ZERO //BigInteger常量0
三、生成BigInteger對象
構造函數
- BigInteger( byte[] val ) //將包含BigInteger的二進制補碼表示形式的byte數組轉換為BigInteger
- BigInteger( int signum , byte[] magnitude ) //byte數組同上一條,signum表示符號:1為正,-1為負,0為零(signum為0時byte數組不可包含非零字節)
- BigInteger( int bitLength , int certainty ,Random rnd ) //生成BigInteger偽隨機數,它可能是(概率不小於1 - 1/2certainty)一個具有指定 bitLength 的素數
- BigInteger( int numBits , Random rnd ) //生成BigInteger偽隨機數,其值均勻分布於[ 0 , 2numBits - 1 )
- BigInteger( String val ) //將 BigInteger 的十進制字符串表示形式轉換為 BigInteger(可包含前導負號)
- BigInteger( String val ,int radix ) //將指定基數(進制)的 BigInteger 的字符串表示形式轉換為 BigInteger
四、常(suo)用(you)方法
算術運算(+ - * / % mod)
- BigInteger add( BigInteger val ) //返回其值為(this + val)的BigInteger
- BigInteger subtract( BigInteger val ) //返回其值為(this - val)的BigInteger
- BigInteger negate() //返回其值是(-this)的BigInteger
- BigInteger multiply( BigInteger val ) //返回其值為(this * val)的BigInteger
- BigInteger divide( BigInteger val ) //返回其值為(this / val)的BigInteger
- BigInteger remainder( BigInteger val ) //返回其值為(this % val)的BigInteger
- BigInteger[] divideAndRemainder( BigInteger val ) //返回包含(this / val)后跟(this % val)的兩個BigInteger的數組
- BigInteger mod( BigInteger m ) //返回其值為(this mod m)的BigInteger(與%不同,其值永遠為正,下同)
- BigInteger modInverse( BigInteger m ) //返回其值為(this-1 mod m)的BigInteger
- BigInteger modPow( BigInteger exponent , BigInteger m ) 返回其值為(thisexponent mod m)的BigInteger(exponent可以為負)
關系運算(== > >= < <= !=)
- int compareTo( BigInteger val ) //將此BigInteger與指定的BigInteger進行比較 ,this>val返回1,this==val返回0,this>val返回-1。使用:(a.compareTo(b) <op> 0),<op>表示關系運算符
- boolean equals( Object x ) //比較此BigInteger與指定的Object的相等性(當且僅當指定的 Object 是一個其值在數字上等於此 BigInteger 的 BigInteger 時,返回 true)
邏輯運算(& | ~ ^ &~)
- BigInteger and( BigInteger val ) //返回其值為(this & val)的BigInteger
- BigInteger or( BigInteger val ) //返回其值為(this | val)的BigInteger
- BigInteger not() //返回其值為(~this)的BigInteger
- BigInteger xor( BigInteger val ) //返回其值為(this ^ val)的BigInteger
- BigInteger andNot(BigInteger val) //返回其值為(this &~ val)的BigInteger
移位運算(<< >>)
- BigInteger shiftLeft( int n ) //返回其值為(this << n)的BigInteger,n可為負,此時相當於執行右移操作
- BigInteger shiftRight( int n ) //返回其值為(this >> n)的BigInteger,執行符號擴展,n可為負,此時相當於執行左移操作
其他二進制運算
- int bitCount() //返回此BigInteger的二進制補碼表示形式中與符號不同的位的數量
- int bitLength() //返回此BigInteger的最小的二進制補碼表示形式的位數,不包括符號位,對於正 BigInteger,這等於常規二進制表示形式中的位數
- boolean testBit( int n ) //當且僅當設置了指定的位時,返回true,即計算((this & (1 << n)) != 0)
- BigInteger setBit( int n ) //返回其值與設置了指定位的此BigInteger等效的BigInteger,即計算(this | (1 << n))
- BigInteger clearBit( int n ) //返回其值與清除了指定位的此BigInteger等效的BigInteger,即計算(this &~ (1 << n))
- BigInteger flipBit( int n ) //返回其值與對此BigInteger進行指定位翻轉后的值等效的BigInteger,即計算(this ^ (1 << n))
- int getLowestSetBit() //返回此BigInteger最右端(最低位)1比特的索引(即從此字節的右端開始到本字節中最右端1比特之間的0比特的位數)如果此 BigInteger 不包含一位,則返回 -1,即計算(this==0? -1 : log2(this & -this))
BigInteger與其他類型轉換
- static BigInteger valueOf( long val ) //返回其值等於指定long型變量值的BigInteger(比long小的基本類型可以轉化為long呦)
- int intValue() //將此BigInteger轉換為int,此轉換類似於高精度變量向低精度變量的轉換(如long到int),下同
- long longValue() //將此BigInteger轉換為long
- float floatValue() //將此BigInteger轉換為float
- double doubleValue() //將此BigInteger轉換為double
- byte[] toByteArray() //返回一個byte數組,該數組包含此BigInteger的二進制補碼表示形式
- String toString() //返回此BigInteger的十進制字符串表示形式
- String toString( int radix ) //返回此BigInteger的給定基數(進制)的字符串表示形式
數學函數
- BigInteger abs() //返回其值是此BigInteger的絕對值的BigInteger
- BigInteger gcd( BigInteger val ) //返回一個BigInteger,其值是abs(this)和abs(val)的最大公約數
- BigInteger pow( int exponent ) //返回其值為(thisexponent)的BigInteger,exponent必須為正數
- BigInteger max( BigInteger val ) //返回此BigInteger和val的最大值
- BigInteger min( BigInteger val ) //返回此BigInteger和val的最小值
- int hashCode() //返回此BigInteger的哈希碼
- boolean isProbablePrime( int certainty ) //如果此BigInteger可能為素數,則返回true,如果它一定為合數,則返回false。(certainty為調用方允許的不確定性的度量,如果該調用返回 true,則此 BigInteger 是素數的概率超出 (1 - 1/2certainty),此方法的執行時間與此參數的值是成比例的)
- BigInteger nextProbablePrime() //返回大於此BigInteger的可能為素數的第一個整數
- static BigInteger probablePrime( int bitLength , Random rnd ) //返回有可能是素數的、具有指定長度bitLength的正BigInteger
- int signum() //返回此BigInteger的正負號,1為正,-1為負,0為零
JAVA API:https://docs.oracle.com/javase/7/docs/api/
一些寫得不錯的相關文章: