Binary, Hexadecimal, Binary Finger Counting Tutorial

kainjow

Registered
While I was thinking of bits and bytes, I was wondering, what would be the most efficient way to determine if a number is even or odd?
Code:
bool isEven(int num)
{
    return ((num & 1) == 0);
}
or
Code:
bool isEven(int num)
{
    return (num % 2 == 0);
}
 

Captain Code

Moderator
Staff member
Mod
Modulus takes a lot of CPU cycles compared to binary AND.
 
Top