Enter a number:16 True
Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Program to Find Whether a Given Number is Power of 2 using Bitwise Operators
C# Program to Find Whether a Given Number is Power of 2 using Bitwise Operators
This is a C# Program to find power of 2 using bitwise operator.
This C# Program Finds Power of 2 using Bitwise Operator.
Here Operations on bits at individual levels can be carried out using Bitwise operations and the whole representation of a number is considered while applying a bitwise operator.
Here is source code of the C# Program to Find Power of 2 using Bitwise Operator. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Find Power of 2 using Bitwise Operator */ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; namespace example { internal class Program { private static void Main(string[] args) { int num; Console.Write("Enter a number:"); num = Convert.ToInt32(Console.ReadLine()); bool result = ((num & -num) == num); Console.WriteLine(result); Console.ReadLine(); } } }
In this C# program, we are reading a number using ‘num’ variable. A bitwise operation is used to operations on bits at individual levels. The whole representation of a number is considered while applying a bitwise operator. Print the value of power of 2.