Huh?
return number % 2 == 0
That’s the only sane solution.
Comment on I wish
DarkMessiah@lemmy.world 1 year ago
Just in case anyone was looking for a decent way to do it…
if (((number/2) - round(number/2)) == 0) return true; else return false;
Or whatever the rounding function is in your language of choice.
Huh?
return number % 2 == 0
That’s the only sane solution.
Do note how I said “a decent” way, not “the best” way. Get that huh outta here.
Or modulo %
private bool IsEven(int number){ return number % 2 ? false : true; }
number % 2 == 0 and // If integer (number & 0b1) == 0
Are the only sane ways to do this. No need to floor.
If you are using floats, you really do not want to have an isEven function …
Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction.
No. The alternative is to not use a float. Testing if a float is even simply does not make sense.
Even testing two floats for equality rarely makes sense.
Take out the else
and I’m in
Valid point.
Acters@lemmy.world 1 year ago
Every bit aside for the ones bit is even, all you have to do is yet the ones bit(the far left) for it being a 1 or 0. Which is the fastest and least amount of code needed.