小编pro*_*101的帖子

for循环中使用的位操作

我在算法的源代码中发现了这个循环.我认为有关这些问题的详细信息与此无关,因为这只是解决方案的一小部分.

    void update(int i, int value, int array[], int n) {
        for(; i < n; i += ~i & (i + 1)) {
            array[i] += value;
        }
}
Run Code Online (Sandbox Code Playgroud)

我真的不明白for循环中会发生什么,这是一种诡计吗?我找到了类似Fenwick树的东西,但它们看起来与我在这里有所不同.

任何想法这个循环意味着什么?

另外,发现了这个:

"Bit Hack#9.隔离最右边的0位.

y = ~x&(x + 1)"

c++ algorithm for-loop bit-manipulation

10
推荐指数
1
解决办法
234
查看次数

标签 统计

algorithm ×1

bit-manipulation ×1

c++ ×1

for-loop ×1