0% found this document useful (0 votes)
70 views4 pages

Count Set Bits in An Integer

The document describes three methods to count the number of 1s in the binary representation of an integer: 1. A simple method that loops through each bit and increments the count if the bit is set, with time complexity of O(logn). 2. Brian Kernighan's algorithm which subtracts 1 from the number and performs a bitwise AND with the original number at each step, toggling the rightmost set bit to 0, with time complexity of O(logn). 3. Using a lookup table to count bits in constant O(1) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views4 pages

Count Set Bits in An Integer

The document describes three methods to count the number of 1s in the binary representation of an integer: 1. A simple method that loops through each bit and increments the count if the bit is set, with time complexity of O(logn). 2. Brian Kernighan's algorithm which subtracts 1 from the number and performs a bitwise AND with the original number at each step, toggling the rightmost set bit to 0, with time complexity of O(logn). 3. Using a lookup table to count bits in constant O(1) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Count set bits in an integer

Write an efficient program to count number of 1s in binary


representation of an integer.

1. Simple Method - Loop through all bits in an integer,


check if a bit is set and if it is then increment the set bit
count. See below program.

/* Function to get no of set bits in binary


representation of passed binary no. */

int countSetBits(unsigned int n)


{
unsigned int count = 0;
while(n)
{
count += n & 1;
n >>= 1;
}
return count;
}

/* Program to test function countSetBits */


int main()
{
int i = 9;
printf("%d", countSetBits(i));

getchar();
return 0;
}

Time Complexity: (-)(logn) (Theta of logn)

2. Brian Kernighans Algorithm:


Subtraction of 1 from a number toggles all the bits
(from right to left) till the rightmost set bit
(including the righmost set bit). So if we subtract
a number by 1 and do bitwise & with itself (n & (n-1)),
we unset the righmost set bit. If we do n & (n-1) in a
loop and count the no of times loop executes we get the
set bit count.
Beauty of the this solution is number of times it loops is
equal to the number of set bits in a given integer.

1 Initialize count: = 0
2 If integer n is not zero
(a) Do bitwise & with (n-1) and assign the value back to n
n: = n&(n-1)
(b) Increment count by 1
(c) go to step 2
3 Else return count

Implementation of Brian Kernighans Algorithm:

#include<stdio.h>

/* Function to get no of set bits in binary


representation of passed binary no. */

int countSetBits(int n)
{
unsigned int count = 0;
while (n)
{
n &= (n-1); n = n & (n-1);
count++;
}
return count;
}

/* Program to test function countSetBits */


int main()
{
int i = 9;
printf("%d", countSetBits(i));
getchar();
return 0;
}

Example for Brian Kernighans Algorithm:

n = 9 (1001)
count = 0

Since 9 > 0, subtract by 1 and do bitwise & with (9-1)

n = 9 & 8 (1001 & 1000)


n=8
count = 1

Since 8 > 0, subtract by 1 and do bitwise & with (8-1)


n = 8 & 7 (1000 & 0111)
n=0
count = 2

Since n = 0, return count which is 2 now.

Time Complexity: O(logn)

3. Using Lookup table: We can count bits in O(1) time using lookup table.
Please see http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable
for details.

You can find one use of counting set bits at http://geeksforgeeks.org/?p=1465


References:
http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive

You might also like