2006/06/27

 

Odd and even numbers

I don't think you'll ever find a better way of detecting whether a number is odd or even. The binary AND operator is brilliant in this setting.

if ((number & 1) == 0); // even
if ((number & 1) == 1); // odd

of course, I believe in C you'd be able to do something like

if (number & 1); // true if odd and false if even

Link: http://www.rgagnon.com/javadetails/java-0488.html

Comments: Post a Comment



<< Home

Copyright © 2006 Stein Gunnar Bakkeby