位數:這條線是怎么工作的?n = n(n - 1);(復制)

[英]Counting number of bits: How does this line work ? n=n&(n-1); [duplicate]


This question already has an answer here:

這個問題已經有了答案:

I need some explanation how this specific line works.
I know that this function counts the number of 1's bits, but how exactly this line clears the rightmost 1 bit?

我需要解釋一下這條線是如何工作的。我知道這個函數計算1位的個數,但是這條線是怎么清除最右邊的1位的呢?

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

Can some explain it to me briefly or give some "proof"?

能不能給我簡單的解釋一下或者給我一些“證據”?

2 个解决方案

#1


16  

Any unsigned integer 'n' will have the following last k digits: One followed by (k-1) zeroes: 100...0Note that k can be 1 in which case there are no zeroes.

任何無符號整數'n'都有以下最后k個數字:1后面跟着(k-1) 0: 100……注意k可以是1,在這種情況下沒有0。

(n - 1) will end in this format: Zero followed by (k-1) 1's: 011...1

(n -1)將以以下格式結束:0,然后(k-1) 1的:011…1

n & (n-1) will therefore end in 'k' zeroes: 100...0 & 011...1 = 000...0

n和(n-1)將因此以k' 0結尾:100……0和011…1 = 000…0

Hence n & (n - 1) will eliminate the rightmost '1'. Each iteration of this will basically remove the rightmost '1' digit and hence you can count the number of 1's.

因此n和(n - 1)將消除最右邊的“1”。每次迭代都會刪除最右邊的“1”位,因此你可以計算1的個數。

#2


3  

I've been brushing up on bit manipulation and came across this. It may not be useful to the original poster now (3 years later), but I am going to answer anyway to improve the quality for other viewers.

我一直在溫習位操作,並偶然發現了這個。這可能對現在(三年后)的原版海報沒有什么用處,但我還是要回答,以提高其他觀眾的質量。

What does it mean for n & (n-1) to equal zero?

n和(n-1)等於0意味着什么?

We should make sure we know that since that is the only way to break the loop (n != 0).Let's say n=8. The bit representation for that would be 00001000. The bit representation for n-1 (or 7) would be 00000111. The & operator returns the bits set in both arguments. Since 00001000 and 00000111 do not have any similar bits set, the result would be 00000000 (or zero).You may have caught on that the number 8 wasn't randomly chosen. It was an example where n is power of 2. All powers of 2 (2,4,8,16,etc) will have the same result.

我們應該確保我們知道,因為這是唯一打破循環的方法(n != 0)假設n=8。它的位表示是00001000。n-1(或7)的位表示是00000111。運算符返回兩個參數中設置的位。由於0000000111和00000000111沒有任何相似的位集,結果將是00000000(或0)。你可能已經意識到數字8不是隨機選擇的。這是一個例子,n是2的冪。2(2,4,8,16等)的所有冪都將得到相同的結果。

What happens when you pass something that is not an exponent of 2? For example, when n=6, the bit representation is 00000110 and n-1=5 or 00000101.The & is applied to these 2 arguments and they only have one single bit in common which is 4. Now, n=4 which is not zero so we increment c and try the same process with n=4. As we've seen above, 4 is an exponent of 2 so it will break the loop in the next comparison. It is cutting off the rightmost bit until n is equal to a power of 2.

如果傳遞的不是2的指數會怎樣?例如,當n=6時,位表示為00000110和n-1=5或00000101。這個&用於這兩個參數,它們只有一個相同的位,就是4。現在,n=4不是0,所以我們增加c,然后對n=4進行同樣的處理。正如我們在上面看到的,4是2的指數,所以它將在下一次比較中打破循環。它切斷了最右邊的位直到n等於2的冪。

What is c?

c語言是什么?

It is only incrementing by one every loop and starts at 0. c is the number of bits cut off before the number equals a power of 2.

它每循環遞增一個,從0開始。c是在數字等於2的冪之前被切斷的比特數。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/03/12/71256e447d15e3645e61b58f6cafeecd.html



n&(n-1)的用途 n&(n-1)的用法 n&(n-1)的妙用 n&(n-1)的用法 n&(n-1)的用處 ((n&(n-1))==0)的含義 n&(n-1)的應用 n&(n-1) 與 n&-n 解釋(n&(n-1))==0的具體含義 n&(n-1)位運算的妙用
 
粤ICP备14056181号  © 2014-2021 ITdaan.com