GCC位扫描 - 转发找到下一个设置位? [英] GCC Bit-scan-forward to find next set bit?
问题描述
我有一个 uint64_t
,我想找到第一个设置位的索引,将它重置为零并找到下一个设置位。
我如何知道何时终止?对所有零的BSF是未定义的...
const uint64_t input = source;
if(0!= input){
int32_t setIndex = GCC_BSF_INTRINSIC(input);
while(setIndex!= UNDEFINED ???){
//做我的逻辑
//复位
input [setIndex ] = 0;
setIndex = BSF_Variant(input);
}
}
有人请帮忙吗?
最简单的方法就是检查输入:
<$ p $(input){
int32_t index = __builtin_ffsll(input); $ p> while(input)
//做东西
}
更复杂的是,根据文档文档:
- 内置函数:
。int __builtin_ffs(int x)
返回1加上x的最低有效1位的索引,或者如果x为零,则返回零
您可以这样做:
for(int index = __builtin_ffsll(input);
index;
index = __builtin_ffsll(input))
{
// do stuff
}
完成同样的事情,你只需重复 __ builtin_ffsll
调用,所以它更加冗长,在我看来并没有贡献澄清。
I have a uint64_t
and I would like to find the index of the first set bit, reset it to zero and find the next set bit.
How do I know when to terminate? BSF on all zeros is undefined...
const uint64_t input = source;
if(0 != input){
int32_t setIndex = GCC_BSF_INTRINSIC(input);
while(setIndex != UNDEFINED???){
//Do my logic
//Reset
input[setIndex] = 0;
setIndex = BSF_Variant(input);
}
}
Could somebody please help?
The simplest would be to just check the input:
while (input) {
int32_t index = __builtin_ffsll(input);
// do stuff
}
More complicatedly, according to the docs the docs:
— Built-in Function:
int __builtin_ffs (int x)
Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
Which lets you do:
for (int index = __builtin_ffsll(input);
index;
index = __builtin_ffsll(input))
{
// do stuff
}
Which accomplishes the same thing, you just have to repeat the __builtin_ffsll
call, so it's more verbose and in my opinion doesn't contribute to clarity.
这篇关于GCC位扫描 - 转发找到下一个设置位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!