Check whether number is even or odd

You can use the modulus operator, but that can be slow. If it’s an integer, you can do:

if ( (x & 1) == 0 ) { even... } else { odd... }

This is because the low bit will always be set on an odd number.

Leave a Comment