Re: Stupid newbie question - determine if integer is even or odd
Re: Stupid newbie question - determine if integer is even or odd
- Subject: Re: Stupid newbie question - determine if integer is even or odd
- From: John Hörnkvist <email@hidden>
- Date: Wed, 14 Aug 2002 01:46:24 +0200
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
_______________________________________________
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.