Odd or even?
Does anyone have a way to determine if a number is odd or even?
En taro Adun!
Doom to all who threaten the homeworld!
*Protoss Zealot - Starcraft*
----------------------------------------------------------You know, I might as well go ahead and say I can't fix the problem... because that's when I figure out how.
Test if the first bit is set. If it is set then the number is odd, otherwise it is even.
A number n (I''ll stick to integers) is even if n mod 2 = 0, and odd if n mod 2 = 1 (these are the only two possibilities, so of course the first test can just be negated for odd). In C (for example), this is n%2 == 0 (even), or n%2 == 1 (odd).
Miles
Miles
yes,
one method is:
if (number MODULO 2) == 1
then
number is odd.
in C++:
if ((number % 2) == 1)
cout << "number is odd";
schiggl
one method is:
if (number MODULO 2) == 1
then
number is odd.
in C++:
if ((number % 2) == 1)
cout << "number is odd";
schiggl
Thanks!
En taro Adun!
Doom to all who threaten the homeworld!
*Protoss Zealot - Starcraft*
En taro Adun!
Doom to all who threaten the homeworld!
*Protoss Zealot - Starcraft*
----------------------------------------------------------You know, I might as well go ahead and say I can't fix the problem... because that's when I figure out how.
um... Miles... don''t the terms odd and even only make sense in relation to integers?
Anyho, the bit test is mucho faster than using modulus, so stick to that.
Anyho, the bit test is mucho faster than using modulus, so stick to that.
Mathematically speaking, they do.
But a float can have a value 2.
so it would be a "integer" in mathamatics
Bugle4d
But a float can have a value 2.
so it would be a "integer" in mathamatics
Bugle4d
~V'lionBugle4d
will the bit test also work with signed integers? negative numbers have inverted bits (2s complement).
a2k
a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement