How to Find if the Number is a Power of 2 using Bitwise Operator in C# ?


Grammarly

More than 40 Lakhs Students and Professionals use Grammarly for their spell and grammar checks. You can register and try for free.

If you need to find out if the number is a Power of 2 using BitWise Operator in C# , below is a sample code snippet that demonstrates how to do it.

How to Find if the Number is a Power of 2 using Bitwise Operator in C# ?

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

namespace AbundantcodeConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int Input = 8;
            bool value = ((Input & -Input) == Input);
            Console.WriteLine(value);
            Console.ReadLine();
        }  
        
    }
    
}
1

Grammarly

More than 40 Lakhs Students and Professionals use Grammarly for their spell and grammar checks. You can register and try for free.
%d bloggers like this: