Hi all,
I'm new over here n want some help. What will be the solution, if we want to find out even/odd number without using divsion or modulus operator?
Best Regards.
This is a discussion on Even/Odd Number without % or / within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm new over here n want some help. What will be the solution, if we want to find ...
Hi all,
I'm new over here n want some help. What will be the solution, if we want to find out even/odd number without using divsion or modulus operator?
Best Regards.
Consider the binary layout of an integer and look up the bitwise & (and) operator.
You can read the number into a string stream, and then using a switch() statement if myStr[myStr.length() - 1] is equal to '1', '3', '5', '7', or '9', then the number is odd. If the number's not odd, then it's even.
Last edited by 7stud; 03-13-2006 at 11:00 PM.
Sure, but don't you think that uses the % operator?
Use two mutually recursive functions!
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.
I know that it does not.Sure, but don't you think that uses the % operator?
Yes, do it the way everyone else does it:Originally Posted by anonytmouse
Code:if (x & 1) { cout << "OMFG x is ODD!!!" << endl; } else { cout << "OMFG x is EVEN!!!" << endl; }
Not quite as efficient as x&1, but guaranteed to work regardless of how an int is represented;
Code:int IsOdd(int x) { while (x > 0) x -= 2; while (x < 0) x += 2; return x; } int IsEven(int x) { return !IsOdd(x); }
You say that as if x&1 won't work with all integers, but it surely will. &1 test for the 1st bit being 1, which for all odd integers this will be the case regardless of sign or type of integer.Originally Posted by grumpy
And since you brought up efficiency, if yours is passed a very large integer, how slow is that going to be? I would guess to slow for most practical purposes.
Last edited by Darryl; 03-14-2006 at 07:03 AM.
Of course, in practice if one suspects that x&1 might not work unless some circumstance, one would just choose x%2... but oranges isnt asking for something that will necessarily be used in practice.And since you brought up efficiency, if yours is passed a very large integer, how slow is that going to be? I would guess to slow for most practical purposes.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
For unsigned integers, x&1 will be the same as x%2. This is also true for positive values of signed integers, whether ones' complement, twos' complement, or sign-and-magnitude is the underlying representation. But negative ones' complement values will produce different results. I think that may have been what grumpy may have meant.Originally Posted by Darryl
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Ok even in theory, I think it's a bad solution :Originally Posted by laserlight
Code:bool isOdd(int n) {return n&1;} bool isOdd(int n) {return n << (8*sizeof(int)-1) ;} bool isOdd(int n) { bitset<sizeof(int)> b(n); return b.test(0);} and for one's complement systems bool isOdd(int n) { return abs(n)&1;}
Last edited by Darryl; 03-14-2006 at 08:21 AM.
Rethink this one too for ones' complement, and for CHAR_BIT != 8, and for padded integers (or is that one just a C possibility?).Code:bool isOdd(int n) {return n << (8*sizeof(int)-1) ;}
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
You've earned the cigar. If our integer type uses ones-complement notation, x&1 will not detect that negative values of x are odd.Originally Posted by Dave_Sinkula
You have knowledge of the inner workings of every STL implementation? Okay...Originally Posted by 7stud
At least in the STL implementation that gcc uses, the modulo operator is in fact used.
http://gcc.gnu.org/onlinedocs/libstd...ce.html#l00883
There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.