there are two well known ways to return an "odd/even" result when analyzing a number.
1 - Usign bitwise operation
Code: Select all
function is_odd($number){
return ($number & 1) ? true:false;
}
(0000 & 0001) -> 0000 -> false
(0001 & 0001) -> 0001 -> true
(0010 & 0001) -> 0000 -> false
(0011 & 0001) -> 0001 -> true
and we have the % Modulus that calculates the Remainder of $a divided by $b
so
Code: Select all
function is_odd($number){
return ($number % 2) ? true:false;
}
Well here's my queastion:
Which one is more efficient? the division or the binary conversion and then the bitwise comparison?
It seems that the division would be better, is it wright?
how could I benchmark this?