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

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: (none)
Date: 10 Jul 1997 20:52:30 GMT
Organization: Oxford University, England
Lines: 28
Message-ID: <5q3i2e$rff@news.ox.ac.uk>
References: <Pine DOT SV4 DOT 3 DOT 93 DOT 970710151909 DOT 18181A-100000 AT giasbga>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


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