Mail Archives: djgpp/1997/07/10/20:21:11
Chirayu Krishnappa (chirayu AT poboxes DOT com) (chirayu AT giasbga DOT vsnl DOT net DOT in) wrote:
: i need to find out if a 4 byte (default) integer has an even number of 1's
: in its binary representation or not. I need to operate on 15Mb data and do
: it fast. shifts (<<) and & is quite slow. is there some lib. function to
: do this? what is the fastest way to get it done?
I don't know how fast it would be, but I think XORing them all together
would be pretty fast:
int get_parity(int *buffer,int bufsize)
{
int a,b,c;
a=0;
for (b=0;b<bufsize;b++) a^=*(buffer++);
c=0;
for (b=0;b<8*sizeof(int);b++) {
c^=a&1;
a>>=1;
}
return c;
}
I expect inline assembler is probably the fastest way though.
--
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford
- Raw text -