If I have a int i = 15;
I know it is 0x00 00 00 0F
in binary is 0000 0000 0000 0000 0000 0000 0000 1111
has 4 1
in binary.
I want to count the sum of 1
in a int variable.
I write this:
int count1(int i)
{
int j = 0,num = 0;
for(;j<32;j++)
if((i<<j )&0x80000000)
num++;
return num;
}
It can work, but I think it is too slow,I mean maybe I have millions int
data. Dose some one have a more effective way to do this?