Sign in to follow this  
streamer

Checking if integer is even or not

Recommended Posts

streamer    415
Helo. I want to check if an integer is even number or not. Is there any safer and faster code than this:
static inline bool CHECKEVEN(int num)
{
	return (num/2 == (float)num/2.0f) ? true : false;
}

Thanks in advance

Share this post


Link to post
Share on other sites
CTar    1134
You should only post one post.


Anyway you can use the modulus operator (like the remainder you most likely have learned in school), so 5%3 = 2 because the remainder is two. So N%2 == 1 for odd and N%2 == 0 for even.

return (N%2==0);

Or if you for some reason prefer the ternary operator:

return ((N%2==0)?true:false);

Share this post


Link to post
Share on other sites
streamer    415
Thank you for your fast reply [smile]. I deleted my first post. I'm accidentaly clicked on refersh button and posted it twice.

Share this post


Link to post
Share on other sites
Prototype    2014
Instead of using a costly modulo oparation, you'd rather check the LSB (least significant bit) with a simple AND. If the result is zero, the number is even.

static inline bool CHECKEVEN(int num)
{
return ((num & 1} == 0);
}

Edit: missed the link that already shows this.

Share this post


Link to post
Share on other sites
streamer    415
Yeah this is very nice [smile] I tried 6 or 7 examples but these are the fastes:

static inline bool CHECKEVEN(int x){
return ((x/2)*2 == x);
}

static inline bool CHECKEVEN(int x){
return ((x&1)==0);
}

for now....

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by streamer
Yeah this is very nice [smile] I tried 6 or 7 examples but these are the fastes:

static inline bool CHECKEVEN(int x){
return ((x/2)*2 == x);
}

static inline bool CHECKEVEN(int x){
return ((x&1)==0);
}

for now....
I don't know if this is standard, but in my experience all caps is usually reserved for macros, enums, and constants. I just mention it because it might make calls to the above functions look a little misleading.

Share this post


Link to post
Share on other sites
streamer    415
It is not standard, but everybody use caps for macros. I'm using too. Firstly this was a macro, and after one year of developing my 'little' program has a bunch of CHECKEVEN calls. And then I turned my macro to proc, and I'm too lazy to rename all calls [smile]

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by streamer
...and I'm too lazy to rename all calls [smile]
Does your IDE have a 'search and replace' feature? :-)

Share this post


Link to post
Share on other sites
streamer    415
yup [smile]
But I used to this function. I love it with caps, and I can't hurt that little proc.

It is with caps mainly because of one reason. I can spot it very fastly when I search throught the code:

if(CHECKEVEN(player.x)) ....

Share this post


Link to post
Share on other sites
Adam Hamilton    271
Quote:
Original post by jyk
I don't know if this is standard, but in my experience all caps is usually reserved for macros, enums, and constants. I just mention it because it might make calls to the above functions look a little misleading.


It would be really unwise to change from ALLCAPS because if for some reason there were a lot of #define macros and some of them were converted to inline procs then trying to remember if certain functions changed their names can be error prone.

Besides, inline procs can be considered macros because the compiler will just insert the code where you invocated the function, just like a #define macro

Share this post


Link to post
Share on other sites
LessBread    1415
Quote:
Original post by Adam Hamilton
Quote:
Original post by jyk
I don't know if this is standard, but in my experience all caps is usually reserved for macros, enums, and constants. I just mention it because it might make calls to the above functions look a little misleading.


It would be really unwise to change from ALLCAPS because if for some reason there were a lot of #define macros and some of them were converted to inline procs then trying to remember if certain functions changed their names can be error prone.

Besides, inline procs can be considered macros because the compiler will just insert the code where you invocated the function, just like a #define macro


jyk is right. Inline functions shouldn't be considered macros. The preprocessor performs macro token substitution which is different than what happens with an inline function.

This substitution happens before the compiler parses the code.

[Edited by - LessBread on March 16, 2006 7:32:54 PM]

Share this post


Link to post
Share on other sites
Adam Hamilton    271
You are right LessBread, macros are different from inline procs in the way a preprocessor handles them but I am right in that you shouldn't change from CHECKEVEN to CheckEven. It is error prone.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Adam Hamilton
You are right LessBread, macros are different from inline procs in the way a preprocessor handles them but I am right in that you shouldn't change from CHECKEVEN to CheckEven. It is error prone.


Right, but for the wrong reasons. A common convention is to use ALL_CAPS for macros that may evaluate their arguments more than once but use theFunctionConvention/the_function_convetion/etc. for "function-like macros" (i.e. macros that evaluate their arguments only once). The main reason for ALL_CAPS macros is to say "this may not do what you expect it to".

I would prefer the change for consistency. But, then again, what do I know, I'm an AP. [grin]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this