What if you could count all the 1s in a number's binary form without checking every single bit?
Why Count Set Bits Brian Kernighan Algorithm in DSA Typescript?
Imagine you have a long list of numbers, and you want to count how many 1s appear in their binary forms. Doing this by checking each bit one by one feels like counting every grain of sand on a beach by hand.
Checking each bit manually takes a lot of time, especially for big numbers. It's easy to make mistakes or get tired, and the process becomes slow and boring.
The Brian Kernighan Algorithm quickly jumps from one 1-bit to the next by removing the rightmost 1 each time. This way, it only counts the actual 1s, skipping all the zeros, making the counting fast and simple.
function countSetBits(number: number): number {
let count = 0;
for (let i = 0; i < 32; i++) {
if ((number & (1 << i)) !== 0) count++;
}
return count;
}function countSetBits(number: number): number {
let count = 0;
while (number !== 0) {
number &= (number - 1);
count++;
}
return count;
}This algorithm enables fast and efficient counting of 1s in binary numbers, even for very large numbers, making bit manipulation tasks much easier.
In networking, counting set bits helps quickly find how many devices are active in a network mask or how many flags are set in a configuration, speeding up system checks.
Manual bit checking is slow and error-prone.
Brian Kernighan Algorithm removes one set bit at a time for fast counting.
It makes bit counting efficient and practical for real-world problems.
Practice
Solution
Step 1: Understand the algorithm's main operation
The Brian Kernighan algorithm clears the least significant set bit in each iteration by doing n = n & (n-1).Step 2: Effect of clearing bits
Each clearing reduces the number of set bits by one until the number becomes zero, counting how many set bits were present.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]
- Confusing bit count with digit count
- Using simple right shifts without bit checks
- Ignoring bitwise AND operation
n?Solution
Step 1: Identify the bitwise operation to clear least set bit
The operation n = n & (n - 1) clears the least significant set bit in n.Step 2: Verify other options
OR, XOR, and addition do not clear the least significant set bit correctly.Final Answer:
n = n & (n - 1); -> Option BQuick Check:
AND with (n-1) clears least set bit [OK]
- Using OR or XOR instead of AND
- Adding numbers instead of bitwise operations
- Missing parentheses around (n - 1)
function countSetBits(n: number): number {
let count = 0;
while (n > 0) {
n = n & (n - 1);
count++;
}
return count;
}
console.log(countSetBits(13));Solution
Step 1: Convert 13 to binary and count set bits
13 in binary is 1101, which has three set bits (1,1,0,1).Step 2: Apply Brian Kernighan algorithm steps
Each iteration clears one set bit, so count increments 3 times until n becomes 0.Final Answer:
3 -> Option AQuick Check:
Number of 1s in 1101 = 3 [OK]
- Counting total bits instead of set bits
- Misunderstanding binary representation
- Off-by-one errors in loop count
function countSetBits(n: number): number {
let count = 0;
while (n != 0) {
n = n & n - 1;
count++;
}
return count;
}Solution
Step 1: Analyze the expression inside the loop
The expression n = n & n - 1 lacks parentheses, so it is evaluated as (n & n) - 1, which is incorrect.Step 2: Correct expression
It should be n = n & (n - 1) to correctly clear the least significant set bit.Final Answer:
Missing parentheses around (n - 1) in the expression. -> Option DQuick Check:
Parentheses ensure correct order of operations [OK]
- Using != 0 causing potential infinite loop for negatives
- Assuming both conditions always equivalent
- Operator precedence confusion causing wrong calculation
[5, 7, 10], which TypeScript code correctly uses Brian Kernighan's algorithm to return an array of their set bit counts?Solution
Step 1: Understand Brian Kernighan's algorithm usage
const counts = arr.map(n => { let count = 0; while (n > 0) { n = n & (n - 1); count++; } return count; }); uses the correct loop with n = n & (n - 1) to count set bits for each number.Step 2: Check other options for correctness
const counts = arr.map(n => n.toString(2).length); counts total binary digits, not set bits. const counts = arr.map(n => { let count = 0; while (n > 0) { n = n >> 1; count++; } return count; }); counts total bits by shifting, not set bits. const counts = arr.map(n => { let count = 0; for (let i = 0; i < 32; i++) { if ((n & (1 << i)) !== 0) count++; } return count; }); uses a loop checking each bit, which works but is not Brian Kernighan's algorithm.Final Answer:
const counts = arr.map(n => { let count = 0; while (n > 0) { n = n & (n - 1); count++; } return count; }); -> Option CQuick Check:
Use n & (n-1) loop inside map for set bits [OK]
- Counting binary string length instead of set bits
- Using right shift without bit check
- Confusing bitwise loop with simple iteration
Ready to prove it?
Pass the test and this pattern moves unknown
