How can I check if a variable (an int) is even (that is, not odd) in C without using "%" operator? I am running on linux platform.
Thank you.
How can I check if a variable (an int) is even (that is, not odd) in C without using "%" operator? I am running on linux platform.
Thank you.
if (!(v & 1))
should be true for even numbers
Try bitwise AND with 0x01 to check if last binary digit is 0 or 1.
In case of 0 it's even, whereas in case of 1 it's odd.
Just for providing an alternative answer, not that this is any better... You could use the following comparison to check whether an int is even (divisible by 2), where asd shall be an int:
asd == asd / 2 * 2; //returns 1 (true) if asd is even