BigInteger

BigInteger 是 Java 提供的一个用于处理任意精度整数的类。它可以用于执行大整数运算,超出了基本数据类型(如 intlong)的范围。

下面是一些常用的 BigInteger 方法和操作:

      1.创建 BigInteger 对象:

BigInteger bigInt1 = new BigInteger("123456789");
BigInteger bigInt2 = BigInteger.valueOf(987654321);

      2.基本运算符和方法:

  • 加法:add(BigInteger val)
  • 减法:subtract(BigInteger val)
  • 乘法:multiply(BigInteger val)
  • 除法:divide(BigInteger val)
  • 求余:remainder(BigInteger val)
  • 求幂:pow(int exponent)
  • 绝对值:abs()
  • 比较:compareTo(BigInteger val)
    BigInteger sum = bigInt1.add(bigInt2);
    BigInteger difference = bigInt1.subtract(bigInt2);
    BigInteger product = bigInt1.multiply(bigInt2);
    BigInteger quotient = bigInt1.divide(bigInt2);
    BigInteger remainder = bigInt1.remainder(bigInt2);
    BigInteger power = bigInt1.pow(2);
    BigInteger absolute = bigInt1.abs();
    int comparison = bigInt1.compareTo(bigInt2);
    

    3.转换为其他数据类型:

  • 转换为 intintValue()
  • 转换为 longlongValue()
  • 转换为 doubledoubleValue()
  • 转换为字符串:toString()
    int intValue = bigInt1.intValue();
    long longValue = bigInt1.longValue();
    double doubleValue = bigInt1.doubleValue();
    String stringValue = bigInt1.toString();
    

    4.其他常用方法:

  • 最大公约数:gcd(BigInteger val)
  • 位操作:bitCount()shiftLeft(int n)shiftRight(int n)
    BigInteger gcd = bigInt1.gcd(bigInt2);
    int bitCount = bigInt1.bitCount();
    BigInteger shiftedLeft = bigInt1.shiftLeft(2);
    BigInteger shiftedRight = bigInt1.shiftRight(3);
    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值