拆分 BigInteger,计算数字?

发布于 2024-12-13 00:20:43 字数 198 浏览 5 评论 0原文

您是否有可能想到一种有效的方法将 BigInteger 分成两半,即如果数字是 12345678,它将被分成 1234?我想我可以将其更改为字符串并使用子字符串,但我不确定这是否是最快的方法。

您还知道如何计算 BigInteger 中的位数吗?我知道您可以执行 bitLength 和 bitCount,但我认为它们是二进制补码。我正在尝试跟踪我将它们分开的位置?

Any chance you might be able to think of an efficient way to split a BigInteger in half, i.e. if the number is 12345678, it would be split into 1234? I was thinking that I could change it into a string and use substring, but I'm not sure if that would be the fastest way to do it.

Do you also know how to count the number of digits in BigInteger? I know that you can do a bitLength and bitCount, but I think those are in two's complements. I'm trying to keep track of where I split them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌酱 2024-12-20 00:20:43
    BigInteger bi = new BigInteger("12345678");
    String numStr = bi.toString();
    System.out.println(numStr.substring(0,numStr.length()/2));
    BigInteger bi = new BigInteger("12345678");
    String numStr = bi.toString();
    System.out.println(numStr.substring(0,numStr.length()/2));
尝蛊 2024-12-20 00:20:43

有人问如何计算二进制数中的十进制位数:https://math.stackexchange.com/questions/3987/how-to-calculate-the-number-of-decimal-digits-for-a-binary-number

看起来这件事并不小。因此,您很可能必须转换为字符串并从那里开始工作。

Some asked how to calculate the number of decimal digits in a binary number here: https://math.stackexchange.com/questions/3987/how-to-calculate-the-number-of-decimal-digits-for-a-binary-number

Seems like its not trivial. So it is very likely that you will have to convert to a String and work from there.

清风夜微凉 2024-12-20 00:20:43

转换为字符串可能是您最好的选择,因为听起来您正在寻找“文本”半点和长度,而这不是“数字”表示提供的。

Converting to String is probably your best bet because it sounds like you're looking for the 'textual' half point and length, which is not something that 'numerical' representations provide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文