• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Stupid newbie question - determine if integer is even or odd
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stupid newbie question - determine if integer is even or odd


  • Subject: Re: Stupid newbie question - determine if integer is even or odd
  • From: Steve Bird <email@hidden>
  • Date: Tue, 13 Aug 2002 19:54:56 -0400

On Tuesday, August 13, 2002, at 07:46 PM, John Hvrnkvist wrote:


On Wednesday, August 14, 2002, at 01:28 AM, Steve Bird wrote:


<Obsessed cycle-counter>
I'd just like to point out that even though everyone else who answered this question did a mod , I did not see a single answer of
if (myVal & 0x1) {
}

--- ANDing is most often faster than division. It pains me to see a division whose result (quotient) is discarded. That's just cruel. There IS such a thing as the Society for the Prevention of Cruelty to Computers,
you know.

</Obsessed cycle-counter>


<compiler writer>

With simple things like this, using modulo should be OK as long as you make it clear what you are up to.

Notice how gcc recognizes the pattern and turns it into a logical operation:

int odd1(int i)
{
return (i % 2);
}

compiles to:

_odd1:
mr r4,r3
srawi r0,r4,1
addze r0,r0
slwi r2,r0,1
subf r3,r2,r4
blr

int odd2(int i)
{
return ((i%2) != 0);
}

compiles to:

_odd2:
rlwinm r3,r3,0,31,31
blr


int odd3(int i)
{
return (i & 1);
}

compiles to:
_odd3:
rlwinm r3,r3,0,31,31
blr


int odd4(int i)
{
return ((i % 2)?1:0);
}

compiles to:

_odd4:
rlwinm r3,r3,0,31,31
blr

</compiler writer>

/John


<Obsessed cycle-counter>
Ah yes. This is a fine job of extracting the kernel from the nut and discarding the chaff.
However, I'll vote for not feeding chaff to compilers everytime.
They eat cycles, too.
It takes time to recognize those patterns.
</Obsessed cycle-counter>


----------------------------------------------------------------
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
www.Culverson.com (toll free) 1-877-676-8175
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Stupid newbie question - determine if integer is even or odd (From: John Hörnkvist <email@hidden>)

  • Prev by Date: Re: Stupid_newbie_question_determine_if_integer is even or odd
  • Next by Date: Re: Stupid newbie question - determine if integer is even or odd
  • Previous by thread: Re: Stupid newbie question - determine if integer is even or odd
  • Next by thread: Re(2): Stupid newbie question - determine if integer is even or odd
  • Index(es):
    • Date
    • Thread