How to Check Even or Odd Number using Bitwise Operator in C# ?

Do you want to find if the number is odd or even in C# ? , using Bitwise AND operator is one of the easiest way to find it.

How to Check Even or Odd Number using Bitwise Operator in C# ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ginktage
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int num = 11;
            bool value = IsEvenNumber(num);
            Console.WriteLine(value);
            Console.ReadLine();
        }
        // Function to Check if the Number is Odd or Even using Bitwise Operator.
        public static bool IsEvenNumber(int num)
        {
            bool value = ((num & 1) == 0);
            return value;
        }    
    }
    
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...