Friday, February 25, 2011

Java program to count the number of bits that are set in an integer

public int setBitCount(int n)
{
//Count the number of bits which are 1

int count=0;

while(n!=0)
{
if((n&1)>0)
{
count++;
}
n=n>>>1;
}
return count;

}

No comments: