gpt4 book ai didi

java - 通过长变量移动Java的BigInteger

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:37 25 4
gpt4 key购买 nike

我知道 BigInteger 类有方法 shiftLeft(int n)shiftRight(int n) 只接受 int 类型参数,但我必须将它移动 long 变量。有什么方法可以做到吗?

最佳答案

BigInteger 只能有 Integer.MAX_VALUE 位。右移超过此值将始终为零。左移除零以外的任何值都会溢出。

来自 Javadoc

 * BigInteger constructors and operations throw {@code ArithmeticException} when
* the result is out of the supported range of
* -2<sup>{@code Integer.MAX_VALUE}</sup> (exclusive) to
* +2<sup>{@code Integer.MAX_VALUE}</sup> (exclusive).

如果您需要超过 20 亿位来表示您的值,那么您会遇到一个相当常见的问题,BigInteger 不是为此而设计的。

如果您需要进行非常大规模的位操作,我建议使用 BitSet[] 这将允许多达 20 亿个位集,超过您的可寻址内存。

yes the long variable might go up to 10^10

对于每个 10^10 位数,您需要 1.25 TB 内存。对于这种大小的数据,您可能需要将其存储在堆之外,我们有一个库可以在不使用太多堆的情况下将如此多的数据保存在单个内存映射中,但是您至少需要在单个磁盘上有这么多的可用空间。 https://github.com/OpenHFT/Chronicle-Bytes

关于java - 通过长变量移动Java的BigInteger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32543395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com