How to Find if an Integer is a Power of 2 in C# ?

Do you want to know if an integer is a power of 2 in C# ? . Below is a simple method with the code snippet that lets you find if the given number is a power of 2 or not.

How to Find if an Integer is a Power of 2 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 InputValue = 16;
            bool value = IsPowerOf2(InputValue);
        // How to Find if the integer is a Power of 2 ?
        private static bool IsPowerOf2(int InputValue)
            bool value = (InputValue & (InputValue - 1)) == 0;
            return value;

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply


Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...