Determining even/odd numbers (integers)?
It's not android specific, but a standard function would be:
boolean isOdd( int val ) { return (val & 0x01) != 0; }
Many compilers convert modulo (%
) operations to their bitwise counterpart automatically, but this method is also compatible with older compilers.
if ((n % 2) == 0) {
// number is even
}
else {
// number is odd
}