Counting the number of set bits in a binary representation of a number is a classic problem in computer science. In binary, a "set bit" is a bit that is equal to 1. Understanding how to count these bits efficiently can prove invaluable in algorithms, competitive programming, and real-world applications like data compression and error detection.
Let's based on the number 13. Its binary representation is:
13 in decimal = 1101 in binary
In this case, we have three set bits: the '1's in positions 0, 2, and 3. The goal is to determine how many such bits exist for any given integer.
The most straightforward method is to iteratively check each bit of the number. Here's how you could do it in Python:
def count_set_bits(n): count = 0 while n: count += n & 1 # Check the least significant bit n >>= 1 # Right shift the bits by 1 position return count # Example: print(count_set_bits(13)) # Output: 3
count
to zero.while
loop, we continue as long as n
is not zero.n
is set (using n & 1
).n
by 1 to check the next bit.n
becomes 0, the loop exits, and we return the count.While this method is simple, it has a time complexity of O(k), where k is the number of bits in the number (which is O(log n) for an integer n).
Brian Kernighan's algorithm is an optimized method for counting set bits. It works by flipping the least significant set bit to 0 and counting how many times we can do this until the number becomes 0.
Here's how you can implement it:
def count_set_bits_bk(n): count = 0 while n: n &= (n - 1) # Clear the least significant set bit count += 1 return count # Example: print(count_set_bits_bk(13)) # Output: 3
count
.while
loop, we continue until n
becomes zero.n & (n - 1)
clears the least significant set bit from n
.This algorithm runs in O(k) time as well, but it tends to perform better since, on average, it will execute fewer iterations than the previous method.
If you need to frequently count set bits for small integers, a great way to optimize is to use a precomputed lookup table. Here’s how you can set it up:
lookup = [0] * 256 for i in range(256): lookup[i] = (i & 1) + lookup[i // 2] def count_set_bits_lookup(n): count = 0 while n: count += lookup[n & 0xff] # Add the set bits from the last 8 bits n >>= 8 # Right shift by 8 bits return count # Example: print(count_set_bits_lookup(13)) # Output: 3
count_set_bits_lookup
function retrieves the number of set bits from the last byte in n
and reduces n
by 8 bits in each iteration until it's zero.Counting set bits has various applications in coding and computational theory:
By choosing the appropriate method for counting set bits based on your specific use case—whether prioritizing simplicity or performance—you can make significant strides in optimizing your algorithms. The world of bit manipulation is vast and powerful, and counting set bits is just the tip of the iceberg!
15/11/2024 | DSA
15/11/2024 | DSA
13/10/2024 | DSA
08/12/2024 | DSA
16/11/2024 | DSA
16/11/2024 | DSA
15/11/2024 | DSA
06/12/2024 | DSA
15/11/2024 | DSA
08/12/2024 | DSA
06/12/2024 | DSA