BIgInteger類理論上可以表示任意位的整數,只要你的計算機內存足夠大;
使用時需要導入 import java.math.BigInteger;
先看構造方法和成員變量:
package 一月十六;
import java.math.BigInteger;
import java.util.Random;
public class Ha {
public static void main(String[] args) {
// TODO Auto-generated method stub
//16進制的a,輸出爲10
BigInteger a=new BigInteger("a",16);
System.out.println(a); //默認是10進制
BigInteger b=new BigInteger("10");
System.out.println(b); //任意數均勻分佈在[0,2^10]
Random ra=new Random();
BigInteger c=new BigInteger(10,ra);
System.out.println(c); //指定位長爲5的隨機素數,是素數的概率爲(1-(1/2)^10)
BigInteger d=new BigInteger(5,10,ra);
System.out.println(d);
//成員變量ONE,TEN,TWO,ZERO; System.out.println(BigInteger.ONE);
System.out.println(BigInteger.TEN);
System.out.println(BigInteger.TWO);
System.out.println(BigInteger.ZERO);
}
}
當然也可自己輸入任意大小的大數
Scanner sc=new Scanner(System.in);
BigInteger a=sc.BigIntegerNext();
成員方法:
BigInteger problePrime(int bitlength,Random rnd);//返回指定位長的質數,錯誤率不超過2^(-100)
public BigInteger nextProbablePrime();//返回下一個可能的質數
public boolean isProbablePrime(int certainty)//判斷是不是質數,錯誤率不超過2^(-certainly);
public static BigInteger valueOf(long val);//把一個long型轉化位大數類型
//加減乘除四則運算
public BigInteger add(BigInteger val);
public BigInteger subtract(BigInteger val);
public BigInteger multiply(BigInteger val);
public BigInteger divide(BigInteger val);//返回結果,捨棄小數
public BigInteger[] divideAndRemainder(BigInteger val);//返回一個長度爲2的數組,一個是商,一個是餘數
public BigInteger remainder(BigInteger val)//返回餘數
public BigInteger pow(int exponent)//指數運算
public BigInteger sqrt()//開方
public BigInteger[] sqrtAndRemainder()//返回開放結果和一個餘數,比如5:返回2,1
public BigInteger gcd(BigInteger val)//最大公約數
public BigInteger abs()//絕對值
public BigInteger negate()//相反數
public int signum()//判斷符號,返回結果只有1,-1,0三種
public BigInteger mod(BigInteger m)//取模,m必須大於零否則拋出異常
public BigInteger modPow(BigInteger exponent,BigInteger m)//返回this^(exponent) mod m,m可以爲複數
public BigInteger modInverse(BigInteger m)//返回this^(-1)mod m
public int compareTo(BigInteger val)//比較兩個數大小,返回只有 0 -1 1
public boolean equals(Object x)//布爾型返回true false
public BigInteger min(BigInteger val)//返回小數
public BigInteger max(BigInteger val)//返回大數
這個在進制轉換中非常有用,無參按10進制返回,有參按radix進制返回
public String toString()
public String toString(int radix)
把大數按相應基本類型返回;
沒有Exact的直接截取相應精度,帶exact 的,在基本類型保存所有數據時拋出異常
public int intValue()
public long longValue()
public float floatValue()
public double doubleValue()
public long longValueExact()
public int intValueExact()
public short shortValueExact()
高級的位移;先說明幾個數的補碼
5 0101
7 0111
-5 1011
-7 1001
4 0100
-4 1010
public BigInteger shiftLeft(int n)//左翼相當於*2
public BigInteger shiftRight(int n)//右移想當於/2
public BigInteger and(BigInteger val)//& 如5&7=5,補碼對應位都爲一纔是1;0101 0111 ——>0111
public BigInteger or(BigInteger val)// | 如-5|-7=-5,補碼對應位有1 就爲1 ;1011 1001->1011 即正常碼1101
public BigInteger xor(BigInteger val)// ^ 如5^7=2,補碼對應位不同才爲1; 0101 0111->0010
public BigInteger not()//取反 ~ ~5=-6,按位取反得到新的補碼0;0101->1010 轉化爲正常碼 1110
public BigInteger andNot(BigInteger val)//this & ~val
public boolean testBit(int n)//從0開始,第n位如果是1,則返回true,否則位false 必須是正數
public BigInteger setBit(int n)//把第n 位設置爲1
public BigInteger clearBit(int n)//把第n位設置位0
public BigInteger flipBit(int n)//把第n 位由0變1 由1 變0
public int getLowestSetBit() //尋找到第一個不爲零數的 0的個數 比如 5 是0,4 是2
public int bitLength()//返回位長,不包含符號位 如4的長度是3 -4的長度 3 即-4 的補碼爲1100 -5的長度爲2
public int bitCount()//補碼錶中和符號位不同的個數;如4 結果爲1,,-4,結果爲 爲3