www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/11/04:14:00

Message-ID: <19970711101328.25319@gil.physik.rwth-aachen.de>
Date: Fri, 11 Jul 1997 10:13:28 +0200
From: Christoph Kukulies <kuku AT gilberto DOT physik DOT rwth-aachen DOT de>
To: Tan Pinghui <ph DOT tan AT bj DOT col DOT co DOT cn>
Cc: "Chirayu Krishnappa (chirayu AT poboxes DOT com)" <chirayu AT giasbga DOT vsnl DOT net DOT in>,
djgpp AT delorie DOT com
Subject: Re:
References: <Pine DOT SV4 DOT 3 DOT 93 DOT 970710151909 DOT 18181A-100000 AT giasbga> <33C5CFF6 DOT 77EB759E AT bj DOT col DOT co DOT cn>
Mime-Version: 1.0
In-Reply-To: <33C5CFF6.77EB759E@bj.col.co.cn>; from Tan Pinghui <ph.tan@bj.col.co.cn> on Fri, Jul 11, 1997 at 03:17:28PM +0900

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019