Message-ID: <19970711101328.25319@gil.physik.rwth-aachen.de> Date: Fri, 11 Jul 1997 10:13:28 +0200 From: Christoph Kukulies To: Tan Pinghui Cc: "Chirayu Krishnappa (chirayu AT poboxes DOT com)" , djgpp AT delorie DOT com Subject: Re: References: <33C5CFF6 DOT 77EB759E AT bj DOT col DOT co DOT cn> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii In-Reply-To: <33C5CFF6.77EB759E@bj.col.co.cn>; from Tan Pinghui on Fri, Jul 11, 1997 at 03:17:28PM +0900 Precedence: bulk On Fri, Jul 11, 1997 at 03:17:28PM +0900, Tan Pinghui wrote: > 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 */ ; > Though the idea is nice, the author of the original mail was talking about a 4 byte integer to be examined and using your method he would be left to do either some pointer manipulation to examine all the bytes or have to do shifts and masks again, being costly either. -- Chris Christoph P. U. Kukulies kuku AT gil DOT physik DOT rwth-aachen DOT de