I wish
I wish
I wish
You're viewing a single thread.
Recently there was a thread trying to declare PHP obsolete.
Hard to beat this in efficiency:
function is_even($num) { return $num % 2 === 0; }
That said, this should work similarly in most languages.
Except maybe in C++ where the makers must have found some bit-fucking method that saves 0.02ms
for an unsigned int, do
(myNum & 1) == 0;
I haven't done binary in years so ill just trust you this works
Checks if least significant bit is equal to zero
Binary reminder
1 - 1
2 - 10
3 - 11
4 - 100
5 - 101
I think you start to see a pattern
Ah yeah, I do remember my basics of hardware classes.
Binary is indeed the easiest and most straightforward way to see number parity. You only need to check one bit.
Its fasicnating that pretty much any function can be rewritten and improved drastically by either bit magic or assembly magic.
If the language you are using uses "real" integers, using a bit mask to get the least significant bit is probably a lot faster - assuming the compiler doesn't replace the operation for you, in which case it doesn't matter.