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

Examples

Input : n = 6
Output : 2
Binary representation of 6 is 110 and has 2 set bits

Input : n = 13
Output : 3
Binary representation of 11 is 1101 and has 3 set bits

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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.

[pastacode lang=”c” manual=”%2F*%20Function%20to%20get%20no%20of%20set%20bits%20in%20binary%0A%20%20%20representation%20of%20passed%20binary%20no.%20*%2F%0Aunsigned%20int%20countSetBits(unsigned%20int%20n)%0A%7B%0A%20%20unsigned%20int%20count%20%3D%200%3B%0A%20%20while(n)%0A%20%20%7B%0A%20%20%20%20count%20%2B%3D%20n%20%26%201%3B%0A%20%20%20%20n%20%3E%3E%3D%201%3B%0A%20%20%7D%0A%20%20return%20count%3B%0A%7D%0A%20%0A%2F*%20Program%20to%20test%20function%20countSetBits%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20i%20%3D%209%3B%0A%20%20%20%20printf(%22%25d%22%2C%20countSetBits(i))%3B%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

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

[ad type=”banner”]

2. Brian Kernighan’s 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 Kernighan’s Algorithm:

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%20%0A%2F*%20Function%20to%20get%20no%20of%20set%20bits%20in%20binary%0A%20%20%20representation%20of%20passed%20binary%20no.%20*%2F%0Aunsigned%20int%20countSetBits(int%20n)%0A%7B%0A%20%20%20%20unsigned%20int%20count%20%3D%200%3B%0A%20%20%20%20while%20(n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20n%20%26%3D%20(n-1)%20%3B%0A%20%20%20%20%20%20count%2B%2B%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20count%3B%0A%7D%0A%20%0A%2F*%20Program%20to%20test%20function%20countSetBits%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20i%20%3D%209%3B%0A%20%20%20%20printf(%22%25d%22%2C%20countSetBits(i))%3B%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Example for Brian Kernighan’s 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)

[ad type=”banner”]

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.

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

Note: In GCC, we can directly count set bits using __builtin_popcount(). So we can avoid a separate function for counting set bits.

[pastacode lang=”c” manual=”%2F%2F%20C%2B%2B%20program%20to%20demonstrate%20__builtin_popcount()%0A%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0Aint%20main()%0A%7B%0A%20%20%20cout%20%3C%3C%20__builtin_popcount%20(4)%20%3C%3C%20endl%3B%0A%20%20%20cout%20%3C%3C%20__builtin_popcount%20(15)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”C Programming” highlight=”” provider=”manual”/]

Output :

1
4
[ad type=”banner”]