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: Steve Bird <email@hidden>
- Date: Tue, 13 Aug 2002 19:48:13 -0400
On Tuesday, August 13, 2002, at 05:57 PM, Jan Van Boghout wrote:
Hi everybody,
I feel kind of embarrassed to ask this, but I looked for hours and still
don't have a solution.
How do you determine whether an integer is odd or even? I tried messing
around with the fact that an odd integer divided by 2 would not be able
to be an integer again, but my attempts were unsuccesful :\ Any help is
welcome!
Thanks,
Jan VB
If you want to chase that particular squirrel, try:
if ((( X>>1) << 1) != X ) {
// it's odd
}
else {
// it's even
}
Which is equivalent (mathematically, if not timewise) to :
if (((X / 2) * 2) != X)
Or how about:
if ( (X << 31) < 0) // assuming 32-bit signed integers
You could maintain a lookup table of all odd numbers and search through
the table to find the number in question....
(Hey! I could work for Microsoft! )
Some implementations of Pascal have a function called Odd()
It does absolutely nothing.
(It accepts an integer in a register, and returns a boolean, which was
defined as a value in bit 0 of the return register)
----------------------------------------------------------------
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.