Best way to find Even/Odd
Whats the best way to find if a number is even or odd. Best, I mean simplist, I could right my own function, by dividing the number by 2, saving as a float, then subtracting it from the number divided by 2 saved as an integer. If its Zero, it even, else its odd. anyway......
Is there a standered function?
Umm how about:
((int)(i)%2)?Odd:Even;
meaning if the remainder of i/2 is 1 then it's odd, and if it's 0 then it's even.
Edited by - Assassin on September 10, 2000 12:49:43 AM
((int)(i)%2)?Odd:Even;
meaning if the remainder of i/2 is 1 then it's odd, and if it's 0 then it's even.
Edited by - Assassin on September 10, 2000 12:49:43 AM
Assassin, aka RedBeard. andyc.org
September 10, 2000 11:55 PM
assumeing that the number is an INTEGER [not a floating point]...the simplest and fastest way to find if it is odd/even is to perform a binary AND with the integer number 1...like:
test = number AND 1
if test = TRUE [or not zero] then the number is odd..else it''s even.
sorry this was in pseudo code [I''m unsure of what language you are useing]...so look up in your compiler the proper syntax for performing binary [AND, OR, NOT, XOR] operations to get the exact code you will need.
test = number AND 1
if test = TRUE [or not zero] then the number is odd..else it''s even.
sorry this was in pseudo code [I''m unsure of what language you are useing]...so look up in your compiler the proper syntax for performing binary [AND, OR, NOT, XOR] operations to get the exact code you will need.
First off I would like to correct your spelling, it''s standard not standered, and secondly no, there is no standard function, the simplest way is to do what Assassin did.
Chris
There is no standard function for testing for oddness or evenness of a given number. Odd and even only has meaning for integer values. Floating point numbers are neither odd nor even unless they are integral, and then you may as well use integers anyway.
Anonymous Poster''s suggestion of using a binary AND with 1 is the easiest way to do it. The result will be 0 for even and 1 for odd. Or if you wanted to use up more CPU cycles than necessary, do a modulus with 2 and see if the result is 0 (even) or 1 (odd).
Quickest: if (value & 1) { // Odd } else { // Even }
Other way: if (value % 2) { // Odd } else { // Even }
Steve 'Sly' Williams
Tools Developer
Krome Studios
Anonymous Poster''s suggestion of using a binary AND with 1 is the easiest way to do it. The result will be 0 for even and 1 for odd. Or if you wanted to use up more CPU cycles than necessary, do a modulus with 2 and see if the result is 0 (even) or 1 (odd).
Quickest: if (value & 1) { // Odd } else { // Even }
Other way: if (value % 2) { // Odd } else { // Even }
Steve 'Sly' Williams
Tools Developer
Krome Studios
Thanks Anonymous and Sly, I didnt think about using AND.
Oh, and BraveZeus, F you 2 peice of CRAP!
WHAT THE HELL IS YOUR PROBLEM. I NEVER TALKED TO ANYONE THATS DOESNT EVEN ANSWER SOMEONES QUESTION AND INSTEAD INSULTS THEM, IM TYPING FAST. I DONT LIKE TO TYPE, SPELLING DOESNT MATTER IF THE READER UNDERSTANDS IT, SO I GUESS YOUR JUST AN IDIOT!
IM NOT AS STUPID AS YOU MIGHT THINK, YES I ALREADY HAVE AN ODD OR EVEN FUNCTION, I WANTED TO MAKE THIS PROGRAM AS SIMPLE AND CLEAN AS IT GETS, AND IVE NOT INSTALLED MSDN YET, SO "SORRY"!
To every other person on this board, thanks for the help,
Oh, and BraveZeus, F you 2 peice of CRAP!
WHAT THE HELL IS YOUR PROBLEM. I NEVER TALKED TO ANYONE THATS DOESNT EVEN ANSWER SOMEONES QUESTION AND INSTEAD INSULTS THEM, IM TYPING FAST. I DONT LIKE TO TYPE, SPELLING DOESNT MATTER IF THE READER UNDERSTANDS IT, SO I GUESS YOUR JUST AN IDIOT!
IM NOT AS STUPID AS YOU MIGHT THINK, YES I ALREADY HAVE AN ODD OR EVEN FUNCTION, I WANTED TO MAKE THIS PROGRAM AS SIMPLE AND CLEAN AS IT GETS, AND IVE NOT INSTALLED MSDN YET, SO "SORRY"!
To every other person on this board, thanks for the help,
Geez, Esap1, take a Valium or something.
Do you really think people are going to help you in future if you go off your nut at someone simply because they help you with your spelling? Maybe English isn't your first language - but I'm always thankful to people who correct my mistakes (whether they're in my English, my Latin, my Spanish or my Japanese).
BraveZeus was right. It IS spelt "standard", and Assassin's method IS the simplest (for integer types).
By the way, remember that the guy you're calling an "idiot" knew the answer to your question. You didn't.
-Hotstone
Edited by - Hotstone on September 11, 2000 10:07:50 PM
Do you really think people are going to help you in future if you go off your nut at someone simply because they help you with your spelling? Maybe English isn't your first language - but I'm always thankful to people who correct my mistakes (whether they're in my English, my Latin, my Spanish or my Japanese).
BraveZeus was right. It IS spelt "standard", and Assassin's method IS the simplest (for integer types).
By the way, remember that the guy you're calling an "idiot" knew the answer to your question. You didn't.
-Hotstone
Edited by - Hotstone on September 11, 2000 10:07:50 PM
Well actually he didnt know the answer, did you read my whole post. Yes it was wrong of me, I was just in a car accident with my friends 5 day old Camero, and I was a little upset, I appolagize(howeva the hell u spell it).
OnTopic:
(n & 1) should be faster than (n % 2). Some compilers might optimize it to the same, but I''m never sure what the % operator boils down to. I find the first more readable, but that''s just a point of personal preference.
OffTopic:
People who criticize others'' spelling errors are doing so because they want to feel superior and end up only being petty. It''s the internet equivalent of "your mama dresses you funny." No rational person would be upset by that insult, so don''t lower yourself to his level.
(n & 1) should be faster than (n % 2). Some compilers might optimize it to the same, but I''m never sure what the % operator boils down to. I find the first more readable, but that''s just a point of personal preference.
OffTopic:
People who criticize others'' spelling errors are doing so because they want to feel superior and end up only being petty. It''s the internet equivalent of "your mama dresses you funny." No rational person would be upset by that insult, so don''t lower yourself to his level.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement