BigInteger

简介: 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);


相关文章
|
云安全 存储 安全
云服务提供商的安全实践:构建可信赖的AWS、Azure和GCP云环境
本篇详细探讨了三家主要云服务提供商,即Amazon Web Services(AWS)、Microsoft Azure和Google Cloud Platform(GCP)的安全实践。我们介绍了每个平台的关键安全功能和工具,以帮助读者构建可信赖的云环境。
564 1
云服务提供商的安全实践:构建可信赖的AWS、Azure和GCP云环境
|
机器学习/深度学习 搜索推荐 人机交互
智能语音交互技术的突破与未来展望###
【10月更文挑战第27天】 本文聚焦于智能语音交互技术的最新进展,探讨了其从早期简单命令识别到如今复杂语境理解与多轮对话能力的跨越式发展。通过深入分析当前技术瓶颈、创新解决方案及未来趋势,本文旨在为读者描绘一幅智能语音技术引领人机交互新纪元的蓝图。 ###
644 0
|
SQL 运维 监控
面经:Presto/Trino高性能SQL查询引擎解析
【4月更文挑战第10天】本文深入探讨了大数据查询引擎Trino(现称Trino)的核心特性与应用场景,适合面试准备。重点包括:Trino的分布式架构(Coordinator与Worker节点)、连接器与数据源交互、查询优化(CBO、动态过滤)及性能调优、容错与运维实践。通过实例代码展示如何解释查询计划、创建自定义连接器以及查看查询的I/O预期。理解这些知识点将有助于在面试中脱颖而出,并在实际工作中高效处理数据分析任务。
961 12
|
移动开发 应用服务中间件 Linux
linux启动tomcat并查看启动日志
linux启动tomcat并查看启动日志
1427 0
|
Shell Go Docker
|
Oracle Java 关系型数据库
Windows环境下JDK环境配置
Windows环境下JDK环境配置
|
大数据 Python
matplotlib Basemap加载shp文件
matplotlib Basemap加载shp文件
287 0