一.BigInteger的构造
package 基础.week2.day10;
import java.math.BigInteger;
import java.util.Random;
public class Demo1 {
public static void main(String[] args) {
//BigInteger的构造
Random r=new Random();
BigInteger bigInteger=new BigInteger(4,r);//随机[0~2^n-1]
System.out.println(bigInteger);
BigInteger bg1=new BigInteger("123");//常用
System.out.println(bg1);
BigInteger bg2=new BigInteger("100",2);//进制
System.out.println(bg2);
BigInteger bg3 = BigInteger.valueOf(100);//value of(long num)
System.out.println(bg3);
}
}
9
123
4
100Process finished with exit code 0
二.BigInteger的常用方法
package 基础.week2.day10;
import java.math.BigInteger;
public class Demo2 {
public static void main(String[] args) {
//BigInteger的常用方法
BigInteger bg1=BigInteger.valueOf(10);
BigInteger bg2=BigInteger.valueOf(5);
BigInteger bg3 = bg1.add(bg2);
System.out.println(bg3);//加
BigInteger bg4=bg1.subtract(bg2);
System.out.println(bg4);//减
BigInteger bg5=bg1.multiply(bg2);
System.out.println(bg5);//乘法
BigInteger bg6=bg1.divide(bg2);
System.out.println(bg6);//除法
BigInteger[] bigIntegers = bg1.divideAndRemainder(bg2);
System.out.println(bigIntegers[0]);//除法 得到商和余数
System.out.println(bigIntegers[1]);
System.out.println(bg1.equals(bg2));//判断是否相同
BigInteger bg7 = bg1.pow(2);//多少次幂
System.out.println(bg7);
BigInteger min = bg1.min(bg2);//最大最小值
BigInteger max = bg1.max(bg2);
System.out.println("min:"+min);
System.out.println("max:"+max);
BigInteger bg=BigInteger.valueOf(1000);
int i = bg.intValue();//转为int类型
System.out.println(i);
}
}
15
5
50
2
2
0
false
100
min:5
max:10
1000Process finished with exit code 0