《加德满都的风铃》:快速遍历变量中所有置1位

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 21:00:44
快速遍历变量中所有置1位 (2010-08-31 16:12) 分类: 算法、心得和多领域技术原理

#include

int main(int argc, char *argv[])
{
    int mask = 0x8039;
    while (mask) {
        int i = 31 - __builtin_clz(mask);
        mask &= ~(1<        printf("[%d]\n", i);
    }
    return 0;
}

lutherge@wiki-server:~/tmp$ ./a.out
[15]
[5]
[4]
[3]
[0]