检查是否设置了n(大)位

发布于 2025-01-19 10:28:58 字数 87 浏览 4 评论 0原文

我想检查是否设置了1024位(128个字节)中的任何一个。如果一切都很清楚,那么我想做一些事情。是否可以快速执行此操作,即一个指令,还是我必须循环浏览我的位图?

I want to check if any of the 1024 bits (128 bytes) are set. If all are clear, then I want to do something. Is it possible to do this quickly, i.e one instruction or do I have to loop through my bitmap?

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

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

发布评论

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

评论(2

泅渡 2025-01-26 10:28:58

正如我所理解的问题,您想要检查是否有任何位是在您拥有的 1024 位中设置的。

假设您使用的是 64 位机器。
将位存储为 unit64_t 类型的数组,即 8 字节

所以你有这种类型的数组。

uint64_t bits[16] = {0}; // 1024 bits

并检查您的钻头状况

for(int i = 0, j=0; i < 16; i++){
    if(bits[i]) {
        return FAIL_CONDITION;
    }
}
return SUCCESS_CONDITION;

As i have understood the questions, you want to check if any of the bit is set out of 1024 bits you have.

Assuming you are on 64 bit machine.
store the bits as array of unit64_t type which is 8 bytes.

So you have array of this kind.

uint64_t bits[16] = {0}; // 1024 bits

and to check your bit condition

for(int i = 0, j=0; i < 16; i++){
    if(bits[i]) {
        return FAIL_CONDITION;
    }
}
return SUCCESS_CONDITION;
不回头走下去 2025-01-26 10:28:58

检查是否有任何数据不为零。
设 buffer 为你要检查的数据,

int n=128;
unsigned char *buffer = &data;
for(int i=0 ;i<n ; i++) {
           if((*data)) {  //If data is non zero any of the bit in the data is 
                          // set, so quit from iterating.
                break;
           } 
           data++;
}

Check whether any data is non zero.
Let buffer be the data you want to check,

int n=128;
unsigned char *buffer = &data;
for(int i=0 ;i<n ; i++) {
           if((*data)) {  //If data is non zero any of the bit in the data is 
                          // set, so quit from iterating.
                break;
           } 
           data++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文