delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/02/15:17:50

From: "Campbell, Rolf [SKY:1U32:EXCH]" <moscoop AT americasm01 DOT nt DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: odd or even?
Date: Thu, 02 Mar 2000 09:45:01 -0500
Organization: Nortel Networks
Lines: 39
Message-ID: <38BE7E6D.6FE78AC9@americasm01.nt.com>
References: <38BE28A9 DOT CD476C62 AT student DOT kuleuven DOT ac DOT be> <38BE4B85 DOT 1F5A0778 AT videotron DOT ca> <l5jsbs87qgu43hr16el30msklmmao5j2h1 AT 4ax DOT com>
NNTP-Posting-Host: wmerh0tk.ca.nortel.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.72 [en] (X11; I; HP-UX B.10.20 9000/785)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Chris Mears wrote:

> That Trancelucid <trancelucid AT videotron DOT ca> really knows where his
> towel is.  On Thu, 02 Mar 2000 06:07:49 -0500, he wrote:
>
> >DAVID JACOBS wrote:
> >>
> >> What is the fastest way to check wether an int/long is odd or even?
> >> I've checked my C/C++ manual, but I didn't find anything....
> >
> >Just mask the bit 0..
> >
> >if(i & 0x01) {
> >   printf("odd");
> >}
> >else {
> >   printf("even");
> >}
>
> I don't know how well this will work with signed integers.

It should work fine with signed integers....
-1 = 0xFFFFFFFF (lowest bit set)
-2 = 0xFFFFFFFE (lowest bit not set)

>  I believe
> the fastest[*] way is:
>
> #define EVEN(x) (((x) % 2) == 0)
> #define ODD(x) (((x) % 2) != 0)

A literal "%" requires a divide (which is slow).  I suspect that with
optimizations on it would be reduced to (x&1) anyways.

--
(\/) Rolf Campbell (\/)



- Raw text -


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