我有兴趣让 GNU Parallel 在 GPU 上运行一些数值计算任务。一般来说,这是我最初的方法:
这就提出了以下问题:
我看到了这段名为“Counting bits set, Brian Kernighan's way”的代码。我很困惑如何“按位与”整数及其减量如何计算设置位,有人可以解释一下吗?
unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
v &= v - 1; // clear the least significant bit set
}
Run Code Online (Sandbox Code Playgroud)