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: "Clark S. Cox III" <email@hidden>
- Date: Tue, 13 Aug 2002 21:00:32 -0400
On 08/13/2002 20:17, "Heather Donahue" <email@hidden> wrote:
>
On 8/13/02 4:28 PM, "Steve Bird" <email@hidden> 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.
>
>
Maybe I'm being pedantic, and possibly even wrong, but doesn't the ANDing
>
method assume you know the internal bitwise representation of the variable?
It does assume that you know something about the bitwise representation.
It's not guaranteed to work on negative numbers (but neither is the modulo
version). The following is safe for both positive and negative numbers
across all conforming C implementations:
If( 1 & (unsigned)x ) {
//number is odd
}
--
Clark S. Cox III
_______________________________________________
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.