Users Online

· Guests Online: 22

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

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.

Problem Description

This C# Program Finds Power of 2 using Bitwise Operator.

Problem Solution

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.

Program/Source Code

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();
        }
    }
}
Program Explanation

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.

 
Runtime Test Cases
 
Enter a number:16
True

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 2.30 seconds
14,558,279 unique visits