"MikkelFJ" <mikkelfj-anti-spam / bigfoot.com> wrote in message
news:3ed255d7$0$97199$edfadb0f / dread12.news.tele.dk...

> > > Perhaps this is an excercise for a new kata?
> >
> > Or for the original one... :)
> >
> > http://pragprog.com/pragdave/Practices/CodeKata
....
> Are you aware that there are efficient boolean operations for counting
bits.
> They are anything but intuitive, but someone figured out the instruction
> sequences and listed them on the internet somewhere that has slipped me
> momentarily. (Something like cryptic like (0xaeaeaeae and myword) or
> 0x121212121 ....).

Here's some source for counting bits. I picked it from an ideal hash table.

// from http://www.jjj.de/bitwizardry/files/bitcount.h

static inline ulong bit_count(ulong x)
{
#if  BITS_PER_LONG == 32
    x -=  (x>>1) & 0x55555555;
    x  = ((x>>2) & 0x33333333) + (x & 0x33333333);
    x  = ((x>>4) + x) & 0x0f0f0f0f;
    x *= 0x01010101;
    return  x>>24;
#endif

#if  BITS_PER_LONG == 64
    x = ((x>>1) & 0x5555555555555555) + (x & 0x5555555555555555);  // 0-2 in
2 bits
    x = ((x>>2) & 0x3333333333333333) + (x & 0x3333333333333333);  // 0-4 in
4 bits
    x = ((x>>4) + x) & 0x0f0f0f0f0f0f0f0f;                         // 0-8 in
4 bits
    x +=  x>> 8;                                                   // 0-16
in 8 bits
    x +=  x>>16;                                                   // 0-32
in 8 bits
    x +=  x>>32;                                                   // 0-64
in 8 bits
    return  x & 0xff;
#endif
}


Mikkel