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

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