bit count in array

Posted by davit-datuashvili on Stack Overflow See other posts from Stack Overflow or by davit-datuashvili
Published on 2010-05-02T19:18:09Z Indexed on 2010/05/02 19:28 UTC
Read the original article Hit count: 194

Filed under:

hello i have following question i know that to count number of set bit in number of number of 1 bit there is following code

int  t ;//in which we want count how many bit are set for instance 3  011   there is 2 bit set
  int count=0;
 while(t>0){
  t&=(t-1);
  count++;
}

now let take array example

int x[]={3,5,6,8,9,7};

i have following code

  int sum=0;
  int count;
  for (int i=0;i<x.length;i++){
   count=0;
  while (x[i]>0){
   x[i]&=(x[i]-1);
   count++;
}
    sum+=count;
}

I have question what is wrong? it doesn't work doesn't show me result;

© Stack Overflow or respective owner

Related posts about algorithms