0 points

Modulus equals zero anyone?

permalink
report
reply
10 points

That’s the joke

permalink
report
parent
reply
1 point
Deleted by creator
permalink
report
parent
reply
8 points

but that operation is expensive

permalink
report
parent
reply
2 points

Nah, it compiles down to an AND because of the constant 2

permalink
report
parent
reply
3 points

So what? It works and it’s better than precompiling a list of all known even and odd numbers, and expecting a computer to iterate through a whole list every time it wants to check the value of something.

The stupid trig tables are just as problematic and it’s why graphics chips use other geometric functions instead. It’s just better to use a modulus.

permalink
report
parent
reply
1 point

No I’m pretty sure an if else ladder is more better

permalink
report
parent
reply
5 points
*

If only you could do something like…

def IsEven(number): return number % 2 == 0

permalink
report
reply
11 points

Can’t do that, we are taking about Java.

permalink
report
parent
reply
3 points

You’re telling me Java doesn’t support the modulo operator?

permalink
report
parent
reply
1 point

Java does have the modulo/remainder support, just like python.

permalink
report
parent
reply
0 points
*
Deleted by creator
permalink
report
reply
3 points

huh? that stupid

permalink
report
parent
reply
7 points

C’mon now, we don’t all have quantum computers to do division

permalink
report
parent
reply
27 points
*

Just in case anyone was looking for a decent way to do it…

if (((number/2) - round(number/2)) == 0) return true;

return false;

Or whatever the rounding function is in your language of choice.

EDIT: removed unnecessary else.

permalink
report
reply
8 points

Huh?

return number % 2 == 0

That’s the only sane solution.

permalink
report
parent
reply
0 points

Do note how I said “a decent” way, not “the best” way. Get that huh outta here.

permalink
report
parent
reply
20 points

Modulo operator my dude.

permalink
report
parent
reply
11 points

Or modulo %

permalink
report
parent
reply
5 points
 private bool IsEven(int number){
    return number % 2 ? false : true;
 }
permalink
report
parent
reply
16 points
*

Every bit aside for the ones bit is even. All you have to do is get the ones bit(the far right) for it being a 1 or 0. Which is the fastest and least amount of code needed.

use bitwise &

// n&1 is true, then odd, or !n&1 is true for even  

 return (!(n & 1));  
permalink
report
parent
reply

Take out the else and I’m in

permalink
report
parent
reply
2 points

Valid point.

permalink
report
parent
reply
5 points
*
number % 2 == 0
and
(number & 0b1) == 0

Are the only sane ways to do this. No need to floor. Although If its C and you can’t modulo floats then (number/2 == floor(number/2))

permalink
report
parent
reply
1 point

If you are using floats, you really do not want to have an isEven function …

permalink
report
parent
reply
1 point

Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction.

permalink
report
parent
reply
9 points
*

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.

permalink
report
reply
17 points

Except maybe in C++ where the makers must have found some bit-fucking method that saves 0.02ms

permalink
report
parent
reply
21 points

for an unsigned int, do

(myNum & 1) == 0;

permalink
report
parent
reply
4 points

I haven’t done binary in years so ill just trust you this works

permalink
report
parent
reply
1 point
*
Deleted by creator
permalink
report
parent
reply
5 points

Binary is indeed the easiest and most straightforward way to see number parity. You only need to check one bit.

permalink
report
parent
reply
1 point

Its fasicnating that pretty much any function can be rewritten and improved drastically by either bit magic or assembly magic.

permalink
report
parent
reply
6 points

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.

permalink
report
parent
reply

Lemmy Shitpost

!lemmyshitpost@lemmy.world

Create post

Welcome to Lemmy Shitpost. Here you can shitpost to your hearts content.

Anything and everything goes. Memes, Jokes, Vents and Banter. Though we still have to comply with lemmy.world instance rules. So behave!


Rules:

1. Be Respectful

Refrain from using harmful language pertaining to a protected characteristic: e.g. race, gender, sexuality, disability or religion.

Refrain from being argumentative when responding or commenting to posts/replies. Personal attacks are not welcome here.


2. No Illegal Content

Content that violates the law. Any post/comment found to be in breach of common law will be removed and given to the authorities if required.

That means:

-No promoting violence/threats against any individuals

-No CSA content or Revenge Porn

-No sharing private/personal information (Doxxing)


3. No Spam

Posting the same post, no matter the intent is against the rules.

-If you have posted content, please refrain from re-posting said content within this community.

-Do not spam posts with intent to harass, annoy, bully, advertise, scam or harm this community.

-No posting Scams/Advertisements/Phishing Links/IP Grabbers

-No Bots, Bots will be banned from the community.


4. No Porn/Explicit

Content


-Do not post explicit content. Lemmy.World is not the instance for NSFW content.

-Do not post Gore or Shock Content.


5. No Enciting Harassment,

Brigading, Doxxing or Witch Hunts


-Do not Brigade other Communities

-No calls to action against other communities/users within Lemmy or outside of Lemmy.

-No Witch Hunts against users/communities.

-No content that harasses members within or outside of the community.


6. NSFW should be behind NSFW tags.

-Content that is NSFW should be behind NSFW tags.

-Content that might be distressing should be kept behind NSFW tags.

If you see content that is a breach of the rules, please flag and report the comment and a moderator will take action where they can.


Also check out:

Partnered Communities:

1.Memes

2.Lemmy Review

3.Mildly Infuriating

4.Lemmy Be Wholesome

5.No Stupid Questions

6.You Should Know

7.Comedy Heaven

8.Credible Defense

9.Ten Forward

10.LinuxMemes (Linux themed memes)


Reach out to

All communities included on the sidebar are to be made in compliance with the instance rules. Striker

Community stats

  • 14K

    Monthly active users

  • 11K

    Posts

  • 256K

    Comments