gpt4 book ai didi

c++ - 计数位数 : How does this line work ? n=n&(n-1);

转载 作者:可可西里 更新时间:2023-11-01 16:28:13 25 4
gpt4 key购买 nike

<分区>

我需要一些解释这个特定行是如何工作的。
我知道这个函数计算的是 1 的位数,但是这一行究竟是如何清除最右边的 1 位的呢?

int f(int n) {
int c;
for (c = 0; n != 0; ++c)
n = n & (n - 1);
return c;
}

有没有人可以简单的给我解释一下或者给出一些“证明”?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com