Odds or Even baby...
I was trying out some point in poly math when I realized I have no idea how to check to see if a number is odd or even...Boy that sounds stupid Is there a function in the stdlib that can do it or will I need to work it out myself.
I prefer
Edited by - VolkerG on May 28, 2000 7:35:59 AM
if(number & 1) number is oddelse number is even
Edited by - VolkerG on May 28, 2000 7:35:59 AM
quote: Original post by VolkerG
I preferif(number & 1) number is oddelse number is even
I prefer to use this too, because it''s faster (unless compilers convert number%2 to number&1 )
-my 2 cents-
--------------------------Programmers don't byte, they nibble a bit. Unknown Person-------------------------
Very good point, VolkerG and Arjan. As you can see, it was late and I wasn''t thinking too hard.
quote: Original post by ArjanOriginal post by VolkerG
I preferif(number & 1) number is oddelse number is even
I prefer to use this too, because it''s faster (unless compilers convert number%2 to number&1 )
-my 2 cents-
Well, I always assume the my compiler is clever enough to do this kind of optimization. Usually, MODs are computed via bitwise ANDs.
If prefer to keep the code mode readable…
Bye,
Karmalaa
---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
In general a compiler will only optimize a modulus of a power of two to a mask if the variable that is being operated on is unsigned. If signed then the "equivalent" masking operation will not produce the same output for negative numbers. The modulus operator when performed on negative signed ints is supposed to be 0 or negative and a masking op will produce a positive number. Therefore the modulus operation will not be replaced with a mask by the compiler.
SiCrane, is there a way I can get your knowledge digitised, and copied into my brain? ;-)
It sure would be useful to know all those little things. I''ve only just gotten back into what the compiler optimises for you, and how to make sure your code is written so that is DOES do the optimisation.
#pragma DWIM // Do What I Mean!
~ Mad Keith ~
It sure would be useful to know all those little things. I''ve only just gotten back into what the compiler optimises for you, and how to make sure your code is written so that is DOES do the optimisation.
#pragma DWIM // Do What I Mean!
~ Mad Keith ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
i think visual c automatically changes %2 to &1 (it''s listed in the optimization list)
- pouya
--------------
The trick to flight is to throw yourself at the ground and miss!!!
- pouya
--------------
The trick to flight is to throw yourself at the ground and miss!!!
May 29, 2000 04:57 PM
Here''s how I check for odd/even in C++:
Please be advised that this is a joke and I do not write my code using these!
class isEven {private: int value;public: even(int num) : value(num) { } operator bool() { return 0 == (value % 2); }};class isOdd {private: int value;public: even(int num) : value(num) { } operator bool() { return !isEven(value); }};int x = ...;if (isEven(x)) { // x is even}else { // x is odd}
Please be advised that this is a joke and I do not write my code using these!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement