delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/11/03:20:34

Message-ID: <33C5CFF6.77EB759E@bj.col.co.cn>
Date: Fri, 11 Jul 1997 15:17:28 +0900
From: Tan Pinghui <ph DOT tan AT bj DOT col DOT co DOT cn>
MIME-Version: 1.0
To: "Chirayu Krishnappa (chirayu AT poboxes DOT com)" <chirayu AT giasbga DOT vsnl DOT net DOT in>
CC: djgpp AT delorie DOT com
Subject: Re:
References: <Pine DOT SV4 DOT 3 DOT 93 DOT 970710151909 DOT 18181A-100000 AT giasbga>

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 */ ;


- Raw text -


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