counting 1 bits SPOILER
This spoiler/nodeshell rescue is in reference to Ariels's writeup in "counting 1 bits."
Sick of staring at that ampersand? Bitter at binary? Let's start by reviewing the given code.
for( n = 0; b; n++ )
b &= (b - 1);
What we've got here is a loop that counts the number of "1"bits in a given number b. After this loop runs, b will equal zero and n will equal the number of 1 bits in b. The tricky part of that loop is the bitwise AND operator, the &=. Given two numbers, a bitwise AND will examine each bit in each number and set the corresponding bit in a result to 1 if both bits in the given numbers were 1; or in any other case 0. If you're not already familiar with this, you really ought to read Wharfinger's writeup for bitwise.
So how does it work? Let us unroll that loop! Set b equal to 45 (0010 1101) and let 'er rip.
n b
1 0010 1101
2 0010 1100
3 0010 1000
4 0010 0000
0000 0000 (loop stops)
And so the loop runs four times, clearing the lowest bit each time through. Still not getting it? Look at (b-1) in comparison to b.
9: 0000 1001 27: 0001 1011
8: 0000 1000 26: 0001 1010
42: 0010 1010 116: 0111 0100
41: 0010 1001 115: 0111 0011
What's going on here? When you subtract 1 from a binary number, you're really turning off the rightmost 1 and turning on every bit after it. Toggling them all, if you will. Let's explicate the loop again. This time, pay close attention to what happens when we logically AND the two numbers together.
n b
0 0010 1101
--------------
0010 1101
& 0010 1100
= 0010 1100
1
--------------
0010 1100
& 0010 1011
= 0010 1000
2
--------------
0010 1000
& 0010 0111
= 0010 0000
3
--------------
0010 0000
& 0001 1111
= 0000 0000
4
--------------
Each iteration through the loop, everything from the least significant 1 bit to the least significant bit is ANDed with its inverse, setting them and every bit betwixt the two to zero. Since the loop eats precisely one bit at a time, it will run until all of the bits have been turned to zeros, leaving n equal to the number of times the loop was processed, and thus counting the 1 bits in that given number b. No nibbles were harmed during the creation of this spoiler.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【each 문 중첩 거동】 each 안에 each 안에 each왜 each 안에 each를 넣고 싶어졌는가 하면 이런 식으로 계층의 카테고리 기능을 작성하는 과정에서 필요하다고 생각했습니다. 내용이 이런 느낌으로 어려워지고 있습니다. 카테고리 수는 모두 400 가까이 ... 유...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.