Count number of bits in a number

Submitted by:Pitter Alderson

Date added:07 June, 2015

Category:C++

Portable code to count the number of bits

Tags: count , count bits , portable code

Code Snippet:

#include <stdio.h>

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

Comments