- Java.math 包其他內容
- Java.math - 列舉
- Java.math - 討論
Java.math.BigInteger 類
簡介
java.math.BigInteger 類提供類似於 Java 所有基本整數運算子以及 java.lang.Math 中所有相關方法的運算。
它還提供用於模運算、GCD 計算、素數測試、素數生成、位操作以及其他一些雜項運算的操作。所有操作的行為都好像 BigInteger 以二進位制補碼錶示。
算術運算和按位邏輯運算的語義分別類似於 Java 的整數算術運算子和 Java 的按位整數運算子。移位運算的語義擴充套件了 Java 的移位運算子,以允許負移位距離。
比較運算執行帶符號整數比較。提供模運算來計算餘數、執行冪運算和計算乘法逆元。位運算對運算元的二進位制補碼錶示中的單個位進行操作。
此類中的所有方法和建構函式在為任何輸入引數傳遞空物件引用時都會丟擲 NullPointerException。
類宣告
以下是java.math.BigInteger類的宣告:
public class BigInteger
extends Number
implements Comparable<BigInteger>
欄位
以下是java.math.BigInteger類的欄位:
static BigInteger ONE - BigInteger 常量一。
static BigInteger TEN - BigInteger 常量十。
static BigInteger ZERO - BigInteger 常量零。
類建構函式
| 序號 | 建構函式 & 描述 |
|---|---|
| 1 | BigInteger(byte[] val) 此建構函式用於將包含 BigInteger 的二進位制補碼錶示的位元組陣列轉換為 BigInteger。 |
| 2 | BigInteger(int signum, byte[] magnitude) 此建構函式用於將 BigInteger 的符號-幅度表示轉換為 BigInteger。 |
| 3 | BigInteger(int bitLength, int certainty, Random rnd) 此建構函式用於構造一個隨機生成的正 BigInteger,它可能是素數,具有指定的 bitLength。 |
| 4 | BigInteger(int numBits, Random rnd) 此建構函式用於構造一個隨機生成的 BigInteger,均勻分佈在 0 到 (2numBits - 1)(包含)的範圍內。 |
| 5 | BigInteger(String val) 此建構函式用於將 BigInteger 的十進位制字串表示轉換為 BigInteger。 |
| 6 | BigInteger(String val, int radix) 此建構函式用於將指定基數中 BigInteger 的字串表示轉換為 BigInteger。 |
類方法
| 序號 | 方法 & 描述 |
|---|---|
| 1 | BigInteger abs()
此方法返回一個 BigInteger,其值為該 BigInteger 的絕對值。 |
| 2 | BigInteger add(BigInteger val)
此方法返回一個 BigInteger,其值為 (this + val)。 |
| 3 | BigInteger and(BigInteger val)
此方法返回一個 BigInteger,其值為 (this & val)。 |
| 4 | BigInteger andNot(BigInteger val)
此方法返回一個 BigInteger,其值為 (this & ~val)。 |
| 5 | int bitCount()
此方法返回該 BigInteger 的二進位制補碼錶示中與其符號位不同的位的數量。 |
| 6 | int bitLength()
此方法返回該 BigInteger 的最小二進位制補碼錶示中的位數,不包括符號位。 |
| 7 | BigInteger clearBit(int n)
此方法返回一個 BigInteger,其值等效於該 BigInteger 且指定位已清除。 |
| 8 | int compareTo(BigInteger val)
此方法將該 BigInteger 與指定的 BigInteger 進行比較。 |
| 9 | BigInteger divide(BigInteger val)
此方法返回一個 BigInteger,其值為 (this / val)。 |
| 10 | BigInteger[ ] divideAndRemainder(BigInteger val)
此方法返回一個包含兩個 BigInteger 的陣列,其中包含 (this / val) 後跟 (this % val)。 |
| 11 | double doubleValue()
此方法將該 BigInteger 轉換為 double。 |
| 12 | boolean equals(Object x)
此方法將該 BigInteger 與指定的 Object 進行相等性比較。 |
| 13 | BigInteger flipBit(int n)
此方法返回一個 BigInteger,其值等效於該 BigInteger 且指定位已翻轉。 |
| 14 | float floatValue()
此方法將該 BigInteger 轉換為 float。 |
| 15 | BigInteger gcd(BigInteger val)
此方法返回一個 BigInteger,其值為 abs(this) 和 abs(val) 的最大公約數。 |
| 16 | int getLowestSetBit()
此方法返回該 BigInteger 中最右端(最低位)的 1 位的索引(最右端 1 位右側的 0 位數)。 |
| 17 | int hashCode()
此方法返回該 BigInteger 的雜湊碼。 |
| 18 | int intValue()
此方法將該 BigInteger 轉換為 int。 |
| 19 | boolean isProbablePrime(int certainty)
此方法如果該 BigInteger 可能是素數則返回 true,如果該 BigInteger 絕對是合數則返回 false。 |
| 20 | long longValue()
此方法將該 BigInteger 轉換為 long。 |
| 21 | BigInteger max(BigInteger val)
此方法返回該 BigInteger 和 val 中較大的一個。 |
| 22 | BigInteger min(BigInteger val)
此方法返回該 BigInteger 和 val 中較小的一個。 |
| 23 | BigInteger mod(BigInteger m)
此方法返回一個 BigInteger,其值為 (this mod m)。 |
| 24 | BigInteger modInverse(BigInteger m)
此方法返回一個 BigInteger,其值為 (this-1 mod m)。 |
| 25 | BigInteger modPow(BigInteger exponent, BigInteger m)
此方法返回一個 BigInteger,其值為 (thisexponent mod m)。 |
| 26 | BigInteger multiply(BigInteger val)
此方法返回一個 BigInteger,其值為 (this * val)。 |
| 27 | BigInteger negate()
此方法返回一個 BigInteger,其值為 (-this)。 |
| 28 | BigInteger nextProbablePrime()
此方法返回大於該 BigInteger 的第一個可能是素數的整數。 |
| 29 | BigInteger not()
此方法返回一個 BigInteger,其值為 (~this)。 |
| 30 | BigInteger or(BigInteger val)
此方法返回一個 BigInteger,其值為 (this | val)。 |
| 31 | BigInteger pow(int exponent)
此方法返回一個 BigInteger,其值為 (thisexponent)。 |
| 32 | static BigInteger probablePrime(int bitLength, Random rnd)
此方法返回一個可能是素數的正 BigInteger,具有指定的 bitLength。 |
| 33 | BigInteger remainder(BigInteger val)
此方法返回一個 BigInteger,其值為 (this % val)。 |
| 34 | BigInteger setBit(int n)
此方法返回一個 BigInteger,其值等效於該 BigInteger 且指定位已設定。 |
| 35 | BigInteger shiftLeft(int n)
此方法返回一個 BigInteger,其值為 (this << n)。 |
| 36 | BigInteger shiftRight(int n)
此方法返回一個 BigInteger,其值為 (this >> n)。 |
| 37 | int signum()
此方法返回該 BigInteger 的符號函式。 |
| 38 | BigInteger subtract(BigInteger val)
此方法返回一個 BigInteger,其值為 (this - val)。 |
| 39 | boolean testBit(int n)
此方法當且僅當指定位已設定時返回 true。 |
| 40 | byte[ ] toByteArray()
此方法返回一個包含該 BigInteger 的二進位制補碼錶示的位元組陣列。 |
| 41 | String toString()
此方法返回該 BigInteger 的十進位制字串表示。 |
| 42 | String toString(int radix)
此方法返回給定基數中該 BigInteger 的字串表示。 |
| 43 | static BigInteger valueOf(long val)
此方法返回一個 BigInteger,其值等於指定的 long 的值。 |
| 44 | BigInteger xor(BigInteger val)
此方法返回一個 BigInteger,其值為 (this ^ val)。 |