nave@lemmy.zip to Lemmy Shitpost@lemmy.worldEnglish · 1 year agoI wishi.imgur.comimagemessage-square188fedilinkarrow-up1930arrow-down118
arrow-up1912arrow-down1imageI wishi.imgur.comnave@lemmy.zip to Lemmy Shitpost@lemmy.worldEnglish · 1 year agomessage-square188fedilink
minus-squareEinarlinkfedilinkarrow-up13arrow-down4·edit-21 year agoRecently 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.
minus-squareTheBlue22@lemmy.blahaj.zonelinkfedilinkarrow-up17·1 year agoExcept maybe in C++ where the makers must have found some bit-fucking method that saves 0.02ms
minus-squareWilzax@lemmy.worldlinkfedilinkarrow-up21·1 year agofor an unsigned int, do (myNum & 1) == 0;
minus-squareTheBlue22@lemmy.blahaj.zonelinkfedilinkarrow-up4·1 year agoI haven’t done binary in years so ill just trust you this works
minus-squareGutek8134@lemmy.worldlinkfedilinkarrow-up8·edit-21 year agoChecks 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
minus-squareTheBlue22@lemmy.blahaj.zonelinkfedilinkarrow-up2·1 year agoAh yeah, I do remember my basics of hardware classes.
minus-squarebouh@lemmy.worldlinkfedilinkarrow-up5·1 year agoBinary is indeed the easiest and most straightforward way to see number parity. You only need to check one bit.
minus-squareTheBlue22@lemmy.blahaj.zonelinkfedilinkarrow-up1·1 year agoIts fasicnating that pretty much any function can be rewritten and improved drastically by either bit magic or assembly magic.
minus-squarejmcs@discuss.tchncs.delinkfedilinkarrow-up6·1 year agoIf 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.
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.
deleted by creator
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.