检查 BigNum 上是否设置了位

发布于 2024-12-11 18:00:19 字数 507 浏览 0 评论 0原文

我有一个像这样的字符串格式的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 技术交流群。

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

发布评论

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

评论(2

泡沫很甜 2024-12-18 18:00:19

首先,您可以(并且应该)使用库来处理任意精度数字。

在java中你有BigInteger,在C++中你可以使用 Gnu's Big Num

如果你不想这样做(我假设你不是在寻找性能) , 你可以:

  • 将字符串转换为其 2-补码表示形式并检查索引。

  • 创建按位运算,并将具有所需索引的二进制表示形式(例如“0100”)的字符串转换为基数 10。

  • 位移位与除以 2 相同,因此,如果您想要位移 54位, 您应该将该数字除以 2^54。然后,您可以检查数字是偶数还是奇数,如果是偶数,则该位未设置。

如果你使用的是最后一种方法,你可以这样做:

bool bitCheck (number, bitIndex) 
    pow = 2^bitIndex
    shifted = number / pow
    return (shifted % 2) == 0

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:

bool bitCheck (number, bitIndex) 
    pow = 2^bitIndex
    shifted = number / pow
    return (shifted % 2) == 0

ps: If you use gmp, you can check this page

水中月 2024-12-18 18:00:19

将字符串转换为二进制字符串,然后检查第 54 个索引。对于 java,您可以尝试使用 BigInteger 类。

    BigInteger bi = new BigInteger("123456789123456789123456789");
    boolean hasBitSet = bi.equals(bi.setBit(54)); 

编辑

    byte[] b = "123456789123456789123456789".getBytes("US-ASCII");
    int maxIndex = b.length - 1;
    for (int bitIdx = 0; bitIdx < (b.length * 8); bitIdx++) {

        int index =  maxIndex - (bitIdx / 8);
        int remainder = bitIdx % 8;

        boolean hasBitSet = (((b[index] & 0xff)) & (1 << remainder)) != 0;
        System.out.println( bitIdx + (hasBitSet ? " has set" : " has not set") );

    }

Convert your string into binary string and then check the 54th index. For java, you may try BigInteger class instead.

    BigInteger bi = new BigInteger("123456789123456789123456789");
    boolean hasBitSet = bi.equals(bi.setBit(54)); 

Edit:

    byte[] b = "123456789123456789123456789".getBytes("US-ASCII");
    int maxIndex = b.length - 1;
    for (int bitIdx = 0; bitIdx < (b.length * 8); bitIdx++) {

        int index =  maxIndex - (bitIdx / 8);
        int remainder = bitIdx % 8;

        boolean hasBitSet = (((b[index] & 0xff)) & (1 << remainder)) != 0;
        System.out.println( bitIdx + (hasBitSet ? " has set" : " has not set") );

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