How can I find whether a number is even or odd using C#?
Asked Answered
J

5

8

I know that there is already a way to find if a number is even or odd using the modulus (What is the fastest way to find if a number is even or odd?). However, I was wondering if there is a C# function like Math.Even or Math.Odd. Is the only way to do this by using a modulus?

Jorin answered 15/12, 2014 at 16:39 Comment(4)
You probably shouldn't bet if you haven't even looked it up yet. I'll assume you mean whether or not such a method exists within the .NET framework (it's not a language feature): no, not as far as I know.Durazzo
Actually I did do some research, I even provided a link of one of the sites I visited.Jorin
There isn't one that I know of, sorry.Franklinfranklinite
Just to clarify, is the requirement that the C# language include the functionality or that some heap of software compatible with C#, e.g. .NET Framework, might offer some assistance?Yemane
P
23

It may count as cheating, but if you use BigInteger, it has an IsEven method.

As stated in MSDN, calling this method is equivalent to:

value % 2 == 0;

Reference:

Phenformin answered 15/12, 2014 at 16:44 Comment(2)
This may actually make him win his bet ;)Ruhl
@LucasTrzesniewski: If anything, they both can have a good laugh and remain friends. :)Phenformin
D
7

Actually, there are more interesting points, and some other methods to check is number even. When you use %, you should check your values with 0 as was mentioned by others, because comparing with 1 will give the wrong answer with all negative integers.

bool is_odd(int n) {
    return n % 2 == 1; // This method is incorrect for negative numbers
}

bool is_odd(int n) {
    return n % 2 != 0;
}

The second popular way is demonstrated below.

bool is_odd(int n) {
    return x & 1 != 0;
}

This method makes use of the fact that the low bit will always be set on an odd number.

Many people tend to think that checking the first bit of the number is faster, but that is not true for C# (at least). The speed is almost the same and often modulus works even faster.

There is the article where the author tried out all popular ways to check if the number is even and I recommend you to look at the tables that are demonstrated at the bottom of the article.

Deryl answered 2/2, 2016 at 10:33 Comment(2)
What do you mean by "...to check is number even" (seems incomprehensible)? Do you mean "...to check if a number is even" (one deletion and three insertions)? (The last paragraph contains "...check if the number is even".) Or something else?Othaothe
You're right, I meant "to check if a number is even"Deryl
A
5

There is no method in .NET that just calls %2==0 for you. Such a simple method presumably isn't worth their time to implement for you, given that the alternative is literally five characters.

You can of course write your own named method to perform this calculation if you really want to.

Alpenstock answered 15/12, 2014 at 16:40 Comment(3)
@IanWise The increment operator saves quite a bit more than just two characters. In many circumstances it can be a pretty dramatic difference. The fact that the reference to be incremented could be a complex expression, with side effects, but that it is only executed once, can take some doing to replicate.Alpenstock
@IanWise Incrementation can also be used in many many more places than "check for eve/odd" - dive deep enough into iteration, particularly with longer variable names, and you'll quickly see this happening.Repulsive
You'd need %2==0. It's 5 characters, not 2. This is 2.5 times more complicated ;)Ruhl
B
1

Using (myVal % 2) == 0 is good, but bitwise operators are fast too:

(myVal & 0x1) == 0

Works for positive and negative values, and different types (e.g. 16-bit or 64-bit).

Bohannon answered 24/6, 2022 at 11:48 Comment(0)
S
1

things changed for generic math.

int.IsEvenInteger(x)

(implemented by System.Int32)

Salespeople answered 14/6 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.