检查 BigNum 上是否设置了位
我有一个像这样的字符串格式的bignum:“123456789123456789123456789”,我需要检查是否设置了指定位。该字符串中的组成部分是单个数字。
通常,如果我想检查第 54 位是否已设置,我会这样做: NyNumber&(1<<54)
问题是我正在使用的 bignum 库中没有 AND 或 SHIFT。
所以问题是;如何检查是否在格式类似于任意大小的字符串的数字中设置了位?
编辑:只是为了澄清:我正在使用一种名为 Autoit3 的小型脚本语言以及以下库:http://www.autoitscript.com/forum/topic/83529-bignum-udf它将 BigNums 表示为字符串。
I have a bignum formatted like a string like this: "123456789123456789123456789" and I need to check if a specified bit is set. The components are single digits in this string.
Usually I would do it like this if I wanted to check if bit 54 is set: NyNumber&(1<<54)
The problem is that I don't have AND or SHIFT in the bignum library I'm using.
So the question is; How can I check if a bit is set in a number formatted like a string with arbitrary size?
Edit: Just to clarify: I'm using a small scripting language called Autoit3 with the following library: http://www.autoitscript.com/forum/topic/83529-bignum-udf which represents BigNums as strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您可以(并且应该)使用库来处理任意精度数字。
在java中你有BigInteger,在C++中你可以使用 Gnu's Big Num
如果你不想这样做(我假设你不是在寻找性能) , 你可以:
将字符串转换为其 2-补码表示形式并检查索引。
创建按位与运算,并将具有所需索引的二进制表示形式(例如“0100”)的字符串转换为基数 10。
位移位与除以 2 相同,因此,如果您想要位移 54位, 您应该将该数字除以 2^54。然后,您可以检查数字是偶数还是奇数,如果是偶数,则该位未设置。
如果你使用的是最后一种方法,你可以这样做:
ps: 如果你使用 gmp,你可以 查看此页面
First of all, you can (and should) use a library for handling Arbitrary Precision numbers.
In java you have BigInteger, and in C++ you can use Gnu's Big Num
If you don't want to do this (and I assume you are not looking for performance), you can:
convert the string to it's 2-complement representation and check the index.
create a bitwise and operation and convert the string with the binary representation of what index you want (for example "0100") to base 10.
Bit shifting is the same as dividing by two, so, if you want to bitshift 54 bits, you should divide the number by 2^54. Then, you can just check if the number is even or odd, if it's even, then the bit isn't set.
If you are using the last method, you can do something like:
ps: If you use gmp, you can check this page
将字符串转换为二进制字符串,然后检查第 54 个索引。对于 java,您可以尝试使用
BigInteger
类。编辑:
Convert your string into binary string and then check the 54th index. For java, you may try
BigInteger
class instead.Edit: