Please explain what this function does?

int function(int n)
{
static int i, j;
for(i = 0, j = 0; i < 13; i++)
{
j += (n>>i)&1;
}
return j;
}

.
.
.
int i=0;
for(i = 0; i < st; i++)


{
if(function(i) == k)
.
.
.

Last edited on
It counts the 1 bits of the argument.
Last edited on
So, how many bits is in "n" ?
Technically, just the number of bits with value=1 in the least significant 13 bits of the number. If an int is 16 bits, the top 3 bits are not considered.

The value 5 (binary 0101), would return a result of 2. The value 8 (1000) would return a result of 1. The value 15 (1111) would return a result of 4.
Topic archived. No new replies allowed.