【Java】BigInteger用法

前言

在Java中,由于没有long long类型。如果需要使用比long类型更大的整数数据时,就可以使用BigInteger类,它支持任意精度的整数。

创建BigInteger类型数据

@Test
public void test1() {
    Scanner scan = new Scanner(System.in);
    //1.控制台读入
    BigInteger n1 = scan.nextBigInteger();

    //2.将BigInteger的十进制字符串表示形式转化为BigInteger
    BigInteger n2 = new BigInteger("123");

    //3.将radix进制的字符串转化为十进制的BigInteger
    String str = "1010011010";
    int radix = 2;
    BigInteger n3 = new BigInteger(str, radix);//666
    //如果直接写成如下,就会直接将1010011010看成十进制整数
    BigInteger n3 = new BigInteger(str);//1010011010
}

基本运算

@Test
public void test2() {
    BigInteger x = new BigInteger("24");
    BigInteger y = new BigInteger("12");
    int val = 5;

    //1.加
    BigInteger n1 = x.add(y);
    //2.减
    BigInteger n2 = x.subtract(y);
    //3.乘
    BigInteger n3 = x.multiply(y);
    //4.除
    BigInteger n4 = x.divide(y);
    //5.取模(y不能为负数)
    BigInteger n5 = x.mod(y);
    //6.求余
    BigInteger n6 = x.remainder(y);
    //7.平方(val不能为负数)
    BigInteger n7 = x.pow(val);
    //8.取绝对值
    BigInteger n8 = x.abs();
    //9.取相反数
    BigInteger n9 = x.negate();
}

类型转换

@Test
public void test3() {
    int val1 = 123;
    long val2 = 456;

    //1.int转换为BigInteger类型
    BigInteger n1 = new BigInteger(val1 + "");

    //2.long转换为BigInteger类型
    BigInteger n2 = new BigInteger(val2 + "");

    //3.BigInteger转int
    //method 1:
    //PS:此方法如果此 BigInteger 的值超出 int 类型的范围,则会引发 ArithmeticException。
    int i1 = n1.intValueExact();
    //method 2:
    //PS:如果这个 BigInteger 太大而无法放入 int 中,则只返回低阶 32 位。请注意,此转换可能会丢失有关 BigInteger 值的总体大小的信息,并返回带有相反符号的结果。
    int i2 = n1.intValue();

    //4.BigInteger转long
    //method 1:
    //PS:此方法如果此 BigInteger 的值超出 long 类型的范围,则会引发 ArithmeticException。
    long l1 = n1.longValueExact();
    //method 2:
    //PS:如果这个 BigInteger 太大而无法放入 long 中,则只返回低阶 64 位。请注意,此转换可能会丢失有关 BigInteger 值的总体大小的信息,并返回带有相反符号的结果。
    long l2 = n1.longValue();
}

比较大小

@Test
public void test4() {
    BigInteger b1 = new BigInteger("123");
    BigInteger b2 = new BigInteger("456");

    //1.compareTo():大于返回1,等于返回0,小于返回-1
    int n1 = b1.compareTo(b2);

    //2.max():返回值:BigInteger,其值为 b1 和 b2 中的较大值。如果它们相等,则可以返回任何一个。
    BigInteger bMax = b1.max(b2);

    //3.min():返回值:BigInteger,其值为 b1 和 b2 中的较小值。如果它们相等,则可以返回任何一个。
    BigInteger bMin = b1.min(b2);
}

常量值

PS:源码注释里面 -1 的访问权限已被修改为了private

@Test
public void test5() {
BigInteger zero = BigInteger.ZERO;//0

BigInteger one = BigInteger.ONE;//1
    
BigInteger two = BigInteger.TWO;//2

BigInteger ten = BigInteger.TEN;//10
}

位运算

@Test
public void test6() {
    BigInteger b1 = new BigInteger("123");
    BigInteger b2 = new BigInteger("456");

    //按位与
    BigInteger bAnd = b1.and(b2);

    //按位或
    BigInteger bOr = b1.or(b2);

    //按位异或
    BigInteger bXor = b1.xor(b2);

    //按位取反
    BigInteger bNot = b1.not();

    //左移2位
    BigInteger bShiftLeft = b1.shiftLeft(2);

    //右移2位
    BigInteger bShiftRight = b1.shiftRight(2);
}
  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值