Quote:
Originally Posted by JoeXIII'007
Side note somewhat related: The modulus (%) operator is amazingly useful if you want to find the remainder, for that is what it does. Its even more useful for determining if a given integer is odd or even given you divide by 2 (Example: 66 % 2 gives you 0 since it is even, 65 % 2 gives you 1 since it is odd).
|
A more efficient way of checking oddness or even-ness is to use bitwise AND with 1 (though a smart compiler will probably do this anyway).
Bitwise AND ands together each binary digit of the two inputs.
4 & 1 = 0
5 & 1 = 1
6 & 1 = 0
Binary example:
5 & 1:
0011 <-- 5
0001 <-- 1
0001 <-- 5 & 1