   Find if integer is odd
  Hello,I am having difficulty writing a function to tell if an integer is odd. I have written one, but it''s very very slow. Here is what it looks like:   #define INT_MAX 2147483647bool ifodd(int n) {static int i;for( i = 1; i < INT_MAX; i+=2 ) {if( n == i ) {return true;}}return false;}int main() {int test = 3;if( ifodd(test) == true ) {test += 1;}}This doesn''t look the greatest, but I can''t think of anything simpler. Thank your for your time.- [BDS]StackOverflow 
  Its really easy to tell if an int is odd...just use the modulus operatorint i = rand();if(i%2 == 1){  //the integer is odd}Basically the modulus operator gives the remainder in integer division.  Ex. 5/2 = 2r1  the % operator would return the 1.    [edited by - Addict on March 11, 2004 2:27:11 PM];  You could use modulo (%) or bitwise-and (&).odd=(i%2)==1;odd=i&1; ;  Hehe, wow. Talk about bogo-sort.BD, you''ll probably want to use the modulus operator (%), which returns the remainder when the left-hand-side is divided by the right-hand-side.   "Sneftel is correct, if rather vulgar." --Flarelocke ;  BOOL IsOdd(int x) {   return (x%2)==1;} ;  // this may be faster than mod (%)BOOL IsOdd(int x) {   return (x&1)==1;} ;  You can use the module operator to do this. a%b gives the remainder of a divided by b. So, a%2 returns 1 of a is odd, and 0 if it''s even.Since 2 is a power of 2, you can also use a bitwise AND (&).  a & 1 returns the least significant bit of a, that is a 1 if it''s odd, and a 0 if a is even.I think it is clearer to use a modulo operator, even though a & will be slightly faster. You can assume that your compiler optizes the % to actually use an &. ;  Wow,Thanks for the fast replies and help guys. I never would have thought of that.Cheers!- [BDS]StackOverflow ;  I don''t know if the compiler optimizes this (it probably does) but you guys could all have code that is potentially a bit faster if not.if( (x & 1) == 1 )is faster thanif( (x % 1) == 1 )BUT you can take the == 1 out of both of those. You see.if() will break into the condition if the expression is not 0. So...if( x & 1 )orif( x % 1 )Both of those are perfectly fine, and potentially faster if the compiler doesn''t optimize that...though I''m almost sure it does.inline BOOL IsOdd( const int & X ){    return x & 1;}Might not want to pass a reference...not sure. Also I think there is a _forceinline but I forget that as well. Hope this helped. ;quote:Original post by Anonymous Poster   BOOL IsOdd(int x) {   return (x%2)==1;}This doesn't work for negative numbers due to the way the % operator is defined in C and C++. You'll need to use a !=0 test instead.Note that modulo is not the same mathematical operation as remainder and that the C and C++ % operator does not implement modulo in the mathematical sense.quote:Original post by ProgramMax   if( (x % 1) == 1 )This doesn't work at all because x%1 is always zero.I think a test like if(x & 1) is actually less likely to cause confusion; C and C++ programmers are more likely to be familiar with the semantics of bitwise operations than those of modulo division.   [edited by - spock on March 11, 2004 4:26:06 PM]