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:
I like to build cool things, work with nice people and help others where I can. Currently I'm an engineering manager for a fintech startup and historically a serial founder & freelancer software dev.
Location
München, Deutschland 🇩🇪
Education
The Open University
Work
Engineering Manager @ Deutsche Fintech Solutions GmbH
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
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:Sidenote on
array_reduce
performance: for arrays of length > 1000 elements,array_sum
is faster thanarray_reduce
by 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.