查找整数的集合位

发布于 2024-10-04 09:05:07 字数 966 浏览 4 评论 0原文

好的,有个问题。我已经组装了一个位掩码供选择。基本上我的页面有一个允许多项选择的列表框,将它们存储在整数列表(它们的 ID 值)中。总共有 14 个选择(因此 ID 值为 1-15)。我将其组装成位掩码的原因是因为我不想在数字中进行硬编码,以防我想向数据库表(列表框从中填充)添加选项。另外,我不想向我的 SQL 存储过程发送 14 个参数(因此硬编码为数字 14)。我可以发送这个整数并解构它(后面的步骤)。

但是,现在我需要找出出于另一个原因在我的整数中设置了哪些位。基本上我有财产。 get 从整数列表(从用户选择中获得)组装位掩码,并返回该二进制十进制值的整数。这是我用于构建位掩码的汇编代码。

//optsNum is my integer list. This is the list containing the ID nums of the selections.
//so if the user selects the first, second, and fourth option, the list contains 1,2,4 (count 3)
//typeCount is an integer of the amount of options in the list box
int total = 0;
for (int c = 0; c < optsNum.Count(); ++c)
{
    for (int i = 0; i <= typeCount; i++)
    {
        if ((i + 1) == optsNum[c})
            total += (1 << i);
    }
}
return total;

因此,如果设置了第一个、第二个和第四个,我的整数是 11。这有效,我测试了所有选择,它返回正确的整数/小数值。

现在我需要帮助制定我的设置方法。这需要获取我拥有的小数/整数,找出设置了哪些位并将它们放回列表中。因此,如果我的值是 11,我需要将其放入整数 1、2、4 的列表中。有人可以帮助我吗?

Okay, got a question. I have assembled a bit mask for options. Basically my page has a list box that allows multiple selections that stores them in a list of integers (their ID value). There are 14 total choices (so ID val 1-15). The reason I am assembling this into a bit mask is because I don't want to hard code in a number in case I want to add options to the database table (where the listbox populates from). Also, I do not want to be sending in 14 parameters to my SQL stored procedure (thus hardcoding in the number 14). I can send in this integer and deconstruction it (later step).

However, right now I need to find out which bits are set in my integer for another reason. Basically I have a property. The get assembles the bit mask from a list of integers (gotten from user selection) and returns an integer of that binary decimal value. Here is my assembled code for the building of the bitmask.

//optsNum is my integer list. This is the list containing the ID nums of the selections.
//so if the user selects the first, second, and fourth option, the list contains 1,2,4 (count 3)
//typeCount is an integer of the amount of options in the list box
int total = 0;
for (int c = 0; c < optsNum.Count(); ++c)
{
    for (int i = 0; i <= typeCount; i++)
    {
        if ((i + 1) == optsNum[c})
            total += (1 << i);
    }
}
return total;

So if the first, second, and fourth are set, my integer is 11. This works, I tested for all selections and it is returning the correct integer/decimal value.

Right now I need help making my set method. This needs to take the decimal/integer that I have, find out which bits are set and place those back into the list. So if I have 11 as my value, I need to put into a list of integers 1,2,4. Can anybody help me?

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

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

发布评论

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

评论(1

蓝眸 2024-10-11 09:05:07

您应该使用 BitArray 类 相反;它为您执行按位运算并且具有简单的界面。
如果您永远不需要超过 32 个布尔值,您还可以使用
BitVector32,较小。


要回答您的问题,您需要循环遍历每一位(使用简单的 for 循环)并检查 value & (1 << i) 查看第 i 位是否已设置。

You should use the BitArray class instead; it does the bitwise operations for you and has a simple interface.
If you will never need more than 32 booleans, you can also use the BitVector32 class, which is smaller.


To answer your question, you need to loop over every bit (using a simple a for loop) and check value & (1 << i) to see whether the ith bit is set.

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