Message-ID: <33C5CFF6.77EB759E@bj.col.co.cn> Date: Fri, 11 Jul 1997 15:17:28 +0900 From: Tan Pinghui MIME-Version: 1.0 To: "Chirayu Krishnappa (chirayu AT poboxes DOT com)" CC: djgpp AT delorie DOT com Subject: Re: References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Precedence: bulk Chirayu Krishnappa (chirayu AT poboxes DOT com) wrote: > hi, > > 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? > > thanks. > > Chirayu Krishnappa: > ------------------ > email: chirayu AT poboxes DOT com > Phone: +91 80 3332616. > ---------------------- > -------------------------------------------------------- As someone else has pointed out, the fastest way is using inline assembly to check CPU's parity flag. But if you don't like inline assembly, here is another way to do it, but it's much more slow. /* first define an array of 256 elements */ const unsigned char XXX[256] = { /* 00000000 */ 1, /* 00000001 */ 0, /* 00000010 */ 0, /* 00000011 */ 1, /* 00000100 */ 0, ... /* 11111110 */ 0, /* 11111111 */ 1 }; /* say the 32-bit integer is L */ unsigned char X = BYTE0(L) ^ BYTE1(L) ^ BYTE2(L) ^ BYTE3(L); /* do the check */ if( XXX[X] ) /* L has even number of bit 1 */ ; else /* L has odd number of bit 1 */ ;