I would say modulus operator is too powerful for this task. All you need to do to determine whether the number is odd or even, is to check number's first bit: 0 for even number, 1 for odd:
Bitwise checks are smart but most people don’t understand them and explaining them would’ve made for a longer article so using modulus was easier to show and explain for a larger audience plus it works perfectly well for this use case.
On the performance point I agree but I don’t care much for it since there isn’t any performance issues arisen worth an implementation change “premature optimisation is the root of all evil” and all that.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I would say
modulusoperator is too powerful for this task. All you need to do to determine whether the number is odd or even, is to check number's first bit:0for even number,1for odd:Sidenote on
array_reduceperformance: for arrays of length > 1000 elements,array_sumis faster thanarray_reduceby an order of magnitude.Bitwise checks are smart but most people don’t understand them and explaining them would’ve made for a longer article so using modulus was easier to show and explain for a larger audience plus it works perfectly well for this use case.
On the performance point I agree but I don’t care much for it since there isn’t any performance issues arisen worth an implementation change “premature optimisation is the root of all evil” and all that.