0
0
DSA Pythonprogramming~10 mins

Count Set Bits Brian Kernighan Algorithm in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Count Set Bits Brian Kernighan Algorithm
Start with number n
Check if n != 0?
NoStop
Yes
Remove rightmost set bit: n = n & (n-1)
Increment count
Repeat check
The algorithm repeatedly removes the rightmost 1-bit from the number and counts how many times this happens until the number becomes zero.
Execution Sample
DSA Python
def count_set_bits(n):
    count = 0
    while n != 0:
        n = n & (n - 1)
        count += 1
    return count
Counts how many 1-bits are in the binary form of n by removing the rightmost set bit each loop.
Execution Table
StepOperationn (binary)n (decimal)CountVisual State
1Start11011301101 (13)
2Remove rightmost set bit: n = n & (n-1)11001211100 (12)
3Remove rightmost set bit: n = n & (n-1)1000821000 (8)
4Remove rightmost set bit: n = n & (n-1)0000030000 (0)
5Check n != 0000003Stop: n is zero
💡 n becomes 0 at step 4, loop ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
n (decimal)1312800
n (binary)11011100100000000000
count01233
Key Moments - 3 Insights
Why does the algorithm use n = n & (n-1) to remove the rightmost set bit?
Because n & (n-1) clears the lowest set bit in n, reducing the number of 1s by one each time, as shown in execution_table steps 2 to 4.
Why does the loop stop when n becomes zero?
When n is zero, there are no set bits left to count, so the condition n != 0 fails and the loop ends, as seen in execution_table step 5.
What happens if the input number is zero at the start?
The loop never runs because n != 0 is false initially, so count remains zero, meaning zero set bits.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count after step 3?
A1
B3
C2
D0
💡 Hint
Check the 'count' column in execution_table row for step 3.
At which step does n become zero, causing the loop to stop?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'n (decimal)' column and find when it reaches 0.
If the input n was 8 (1000 in binary), how many times would the loop run?
A1
B2
C3
D4
💡 Hint
Count the number of set bits in 1000 binary; relate to count increments in variable_tracker.
Concept Snapshot
Count Set Bits using Brian Kernighan's Algorithm:
- Initialize count = 0
- While n != 0:
  - n = n & (n-1) removes rightmost set bit
  - Increment count
- Return count
Efficiently counts 1-bits by clearing them one by one.
Full Transcript
This visualization shows how Brian Kernighan's algorithm counts set bits in a number. Starting with n, it repeatedly removes the rightmost 1-bit by computing n = n & (n-1). Each removal increments the count. The process repeats until n becomes zero, meaning no set bits remain. The execution table traces each step with binary and decimal values of n and the count. Key moments clarify why the bit removal works and when the loop stops. The quiz tests understanding of count values and loop termination. This method is efficient because it only loops as many times as there are set bits.