CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Odd or even number

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

Odd or even number
PostPosted: Fri Jul 08, 2005 10:21 am     Reply with quote

This may be a dumb question but what's an easy way to determine if a number(variable) is odd or even? Embarassed

Ronald
Ttelmah
Guest







Re: Odd or even number
PostPosted: Fri Jul 08, 2005 10:25 am     Reply with quote

rnielsen wrote:
This may be a dumb question but what's an easy way to determine if a number(variable) is odd or even? Embarassed

Ronald

For an integer, it couldn't be easier!. Use a bitwise '&' with '1'. If this is non-zero (true), the number is odd.
So:
Code:

#define isodd(x) ((x)&0x1L)

//Use this like:

if (isodd(value)) {
   //Here the number was odd
}


Best Wishes
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

Re: Odd or even number
PostPosted: Fri Jul 08, 2005 10:26 am     Reply with quote

rnielsen wrote:
This may be a dumb question but what's an easy way to determine if a number(variable) is odd or even? Embarassed

Ronald


Assuming an integer, if the LSB is zero, the number is even.

Scott
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 10:26 am     Reply with quote

You just test the LSB.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Fri Jul 08, 2005 10:51 am     Reply with quote

Code:

if (n & 0x01)
{
// odd
}
else
{
// even
}
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 11:06 am     Reply with quote

Thanks. I like the LSB bit test. Simple.

Ronald
Ttelmah
Guest







PostPosted: Fri Jul 08, 2005 3:21 pm     Reply with quote

I have to wonder if this thread was actually a 'record'. Four answers, all with basically the same solution in one minute!. I have seen a couple of overlapped answers before, but four is pretty amazing. :-)
Given that the writers, can't see other answers 'in preparation', it shows a remarkable activity level on the group.

Best Wishes
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Jul 08, 2005 3:49 pm     Reply with quote

Great minds think alike.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group