bool odd(int n) {
/* Returns true is n is odd */
return (n & 1)
}
The logic of it:
An odd number can be written as 2n + 1, where n is an integer (even or odd). Since all powers of two are divisible by two (i.e. can be written as 2n), except 2^0 == 1 which is odd, we can prove that an odd binary number must have its least significant bit set (end with a binary 1).
0 1 0 1 0 0 1 1
0 0 0 0 0 0 0 1 &
----------------
0 0 0 0 0 0 0 1 // The result is the integer 1, which is freely convertable to bool (true) under C++.
This might save you some cycles, although that was probably not an issue to beging with. You could also do this:
return (n % 2);
Which is basically the same thing but perhaps more clear.