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.
Comment on I wish
original_reader@lemm.ee 1 year ago
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 in most languages.
jmcs@discuss.tchncs.de 1 year ago
TheBlue22@lemmy.blahaj.zone 1 year ago
Except maybe in C++ where the makers must have found some bit-fucking method that saves 0.02ms
Wilzax@lemmy.world 1 year ago
for an unsigned int, do
(myNum & 1) == 0;
TheBlue22@lemmy.blahaj.zone 1 year ago
I haven’t done binary in years so ill just trust you this works
Gutek8134@lemmy.world 1 year ago
Checks if least significant bit is equal to zero
bouh@lemmy.world 1 year ago
Binary is indeed the easiest and most straightforward way to see number parity. You only need to check one bit.
TheBlue22@lemmy.blahaj.zone 1 year ago
Its fasicnating that pretty much any function can be rewritten and improved drastically by either bit magic or assembly magic.