Here’s a blog article which benchmarks quite a few ways to test if a number is odd or even: blogs ^ davelozinski ^ com/curiousconsultant/csharp-net-fastest-way-to-check-if-a-number-is-odd-or-even
There are actually numerous ways to do this. Surprisingly, the fastest way appears to be the modulus % operator, even out performing the bitwise ampersand &, as follows:
if (x % 2 == 0)
total += 1; //even number
else
total -= 1; //odd number
Definitely worth a read for those that are curious.
cc davelozinski com/c-sharp/fastest-way-to-check-if-a-number-is-odd-or-even
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.