What if you could count all the 1s in a number without looking at every single bit?
Why Count Set Bits Brian Kernighan Algorithm in DSA C++?
Imagine you have a long list of numbers, and you want to know how many bits are set to 1 in each number. Doing this by checking every single bit one by one feels like counting every grain of sand on a beach by hand.
Checking each bit manually is slow and boring. It wastes time and can easily lead to mistakes, especially with big numbers. It's like trying to find all the red apples in a huge orchard by looking at each apple one by one.
The Brian Kernighan Algorithm cleverly jumps from one set bit to the next without checking every bit. It quickly removes the rightmost set bit in each step, making counting fast and simple.
int countSetBits(int number) {
int count = 0;
while (number > 0) {
if (number & 1) count++;
number >>= 1;
}
return count;
}int countSetBits(int number) {
int count = 0;
while (number > 0) {
number &= (number - 1);
count++;
}
return count;
}This algorithm makes counting set bits in numbers fast and efficient, enabling quick solutions in areas like cryptography, error detection, and graphics.
In networking, quickly counting set bits helps find how many devices are active in a network mask, speeding up network management.
Manual bit checking is slow and error-prone.
Brian Kernighan's method removes one set bit at a time, speeding up counting.
This technique is useful in many fast computing tasks involving bits.
Practice
Solution
Step 1: Understand the algorithm's main operation
The Brian Kernighan algorithm works by clearing the least significant set bit in each iteration.Step 2: Effect of clearing bits
Each clearing reduces the number of set bits by one until the number becomes zero.Final Answer:
It repeatedly clears the least significant set bit until the number becomes zero. -> Option AQuick Check:
Clearing bits one by one counts set bits [OK]
- Counting all bits instead of only set bits
- Using string conversion unnecessarily
- Shifting bits without checking if set
Solution
Step 1: Identify Brian Kernighan's loop
The algorithm uses while (n != 0) and clears the least significant set bit by n = n & (n - 1).Step 2: Confirm increment of count
Each iteration increments count by one for each cleared set bit.Final Answer:
while (n != 0) { n = n & (n - 1); count++; } -> Option CQuick Check:
Loop clears bits and counts [OK]
- Using for loop instead of while
- Counting bits by shifting instead of clearing
- Using modulo instead of bitwise operations
int countSetBits(int n) {
int count = 0;
while (n) {
n = n & (n - 1);
count++;
}
return count;
}
int main() {
int x = 13;
std::cout << countSetBits(x);
return 0;
}Solution
Step 1: Convert 13 to binary
13 in binary is 1101, which has three set bits (1s).Step 2: Apply Brian Kernighan's algorithm
Each iteration clears one set bit, so count increments 3 times before n becomes 0.Final Answer:
3 -> Option BQuick Check:
Number of 1s in 1101 = 3 [OK]
- Counting total bits instead of set bits
- Miscounting binary 1s
- Confusing bitwise AND with OR
int countSetBits(int n) {
int count = 0;
while (n > 0) {
n = n & (n + 1);
count++;
}
return count;
}Solution
Step 1: Check bit clearing operation
The correct operation to clear the least significant set bit is n = n & (n - 1), not n + 1.Step 2: Effect of wrong operation
Using n + 1 does not clear bits properly and causes incorrect count or infinite loop.Final Answer:
Using n + 1 instead of n - 1 in bit clearing step. -> Option DQuick Check:
Correct bit clearing uses n & (n - 1) [OK]
- Replacing n - 1 with n + 1
- Confusing loop condition syntax
- Forgetting to increment count
Solution
Step 1: Understand counting set bits per element
Brian Kernighan's algorithm counts set bits in one integer at a time.Step 2: Apply algorithm to each array element
Iterate through the array, count set bits for each element, and sum these counts for total.Final Answer:
Iterate over each element, apply Brian Kernighan's algorithm to count set bits, and sum all counts. -> Option AQuick Check:
Sum of individual counts gives total set bits [OK]
- Counting bits of sum instead of each element
- Using string conversion unnecessarily
- Assuming uniform bit count per element
Ready to prove it?
Pass the test and this pattern moves unknown
