You're viewing a single thread.
Einar @lemm.ee 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.
13 4 ReplyTheBlue22 @lemmy.blahaj.zone Except maybe in C++ where the makers must have found some bit-fucking method that saves 0.02ms
17 0 ReplyWilzax @lemmy.world for an unsigned int, do
(myNum & 1) == 0;
21 0 ReplyTheBlue22 @lemmy.blahaj.zone I haven't done binary in years so ill just trust you this works
4 0 ReplyGutek8134 @lemmy.world
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
8 0 ReplyTheBlue22 @lemmy.blahaj.zone Ah yeah, I do remember my basics of hardware classes.
2 0 Reply
bouh @lemmy.world Binary is indeed the easiest and most straightforward way to see number parity. You only need to check one bit.
5 0 ReplyTheBlue22 @lemmy.blahaj.zone Its fasicnating that pretty much any function can be rewritten and improved drastically by either bit magic or assembly magic.
1 0 Reply
jmcs @discuss.tchncs.de 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.
6 0 Reply