tags:

views:

123

answers:

1
+1  Q: 

Bit count in array

I know that to count the number of set bits in a number, the following code can do it:

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

Now an array example:

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

I have the 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;
}

This does not work, however. What is wrong?

+2  A: 

Your code works fine for me except that length was undefined - maybe because you were using Java, not C as I first guessed. Anyway I got it working in C:

#include <stdio.h>

int main()
{
    int x[]={3,5,6,8,9,7};
    int sum=0;
    int count;
    for (int i=0;i<6;i++){
        count=0;
        while (x[i]>0){
            x[i]&=(x[i]-1);
            count++;
        }
        sum+=count;
    }

   printf("%d\n", sum);
}

Output:

12

A simpler way is to bitshift in a loop and count the number of bits as they pass by.

count = 0;
while (t)
{
    count += t & 1;
    t >>= 1;
}

This page shows some more advanced algorithms including using a lookup table or a clever parallel algorithm. The method you are using is called "Brian Kernighan's way" on that page.

You could also see what your compiler provides, e.g.:

int __builtin_popcount (unsigned int x);

To avoid the possibility of introducing errors when using this code to get the total number of bits in the array you could keep it as a separate function and call it once per element in the array. This will simplify the code.

Mark Byers
thank you very much i want to say only one stackoverflow is best forum in the world now please i have one question at most forums when somebody put question about programming participant of forums say that this is homework of course sometimes not always and they dont help i have question why not help to solve homework problem? just interesting
@dvit-datuashvili: If you post homework questions here people will help you, but only if you show that you have made an effort to solve it yourself first, which you have done here.
Mark Byers
Because often homework questions are just posted in a way which indicates that the poster did not even think about the question. In a place like this, where people like to volunteer help, they want to know that their effort goes to a good cause, i.e. someone who cares enough about the topic to think about the question.
Joe
nice nice guys i wnat to say that every question which i will put here will be some code or ideas i have not any homework i am not studing at school i am a student and try so study programming very well so please help me as u can when i put here question ok?
If you want good results, be sure to read the FAQ completely first. http://stackoverflow.com/faq
Joe
ok i have question from programming pearls there i saw question such ascompress english online dictionary as small as possible what about term compress?programaticaly is it possibly to make it? for example take words which have the same meaning? and make size of space small? or?
@davit-datuashvili: Post it as a new question. Comments are not a suitable medium for new questions and answers for many reasons.
Mark Byers