在 Kotlin 中将 BigInteger 转换为 ULong

问题描述 投票:0回答:1

给定 Kotlin 中

BigInteger
的范围在 0 到 2^64 之间,如何将这个
BigInteger
转换为
ULong
? Kotlin 提供
toULong()
扩展方法 仅适用于 Byte/Short/Int/Long/Float/Double

kotlin biginteger
1个回答
1
投票

您可以安全地进行

bigint.toLong().toULong()

toLong()
分机呼叫
BigInteger.longValue()
。文档中说“如果这个 BigInteger 太大而无法放入 long 中,则仅返回低位 64 位”。

然后

Long.toULong()
扩展被记录为“生成的 ULong 值与该 Long 值具有相同的二进制表示形式。”

因此,将这两者放在一起,我们可以看到

toLong().toULong()
将保留 BigInteger 在 0 到 2^64-1 之间的所有 64 位。

© www.soinside.com 2019 - 2024. All rights reserved.