Java BigInteger成员方法和构造方法

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章