C# Basic
C# Interview Programs
- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number
C# Program to Check Power of 2
Photo Credit to CodeToFun
π Introduction
In the realm of programming, efficiency and optimization are key considerations. One common optimization involves determining whether a given number is a power of 2.
Checking if a number is a power of 2 is a fundamental operation with applications in various domains.
In this tutorial, we'll explore a C# program that efficiently checks whether a given number is a power of 2.
π Example
Let's delve into the C# code that accomplishes this functionality.
using System;
class Program {
// Function to check if a number is a power of 2
static bool IsPowerOfTwo(int num) {
// A number is a power of 2 if and only if it has a single set bit.
// Using bitwise AND operation to check this condition.
return (num > 0) && ((num & (num - 1)) == 0);
}
// Driver program
static void Main() {
// Replace this value with your desired number
int number = 16;
// Check if the number is a power of 2
if (IsPowerOfTwo(number)) {
Console.WriteLine($"{number} is a power of 2.");
} else {
Console.WriteLine($"{number} is not a power of 2.");
}
}
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the Main method.
16 is a power of 2.
Compile and run the program to see whether the given number is a power of 2.
π§ How the Program Works
- The program defines a function IsPowerOfTwo that takes an integer as input and returns true if the number is a power of 2 and false otherwise.
- Inside the function, it uses a bitwise AND operation to check if the number has a single set bit, which is a characteristic of power-of-2 numbers.
- The Main method demonstrates the usage of the IsPowerOfTwo function by checking if a given number is a power of 2.
π Between the Given Range
Let's dive into the C# code that performs the check for powers of 2 in the range 1 to 20.
using System;
class Program {
// Function to check if a number is a power of 2
static bool IsPowerOfTwo(int num) {
return (num > 0) && ((num & (num - 1)) == 0);
}
// Driver program
static void Main() {
Console.WriteLine("Power of 2 in the range 1 to 20:");
for (int i = 1; i <= 20; i++) {
if (IsPowerOfTwo(i)) {
Console.Write($"{i} ");
}
}
Console.WriteLine();
}
}
π» Testing the Program
Power of 2 in the range 1 to 20: 1 2 4 8 16
The program is set to check powers of 2 in the range of 1 to 20. Run the program to see the output.
π§ How the Program Works
- The program defines a function IsPowerOfTwo that checks whether a number is a power of 2.
- Inside the Main method, it iterates through numbers from 1 to 20.
- For each number, it calls the IsPowerOfTwo function to check if it's a power of 2.
- If it is, the number is printed.
π§ Understanding the Concept of Power of 2
A number is considered a power of 2 if and only if it has a single set bit.
For example, 2, 4, 8, and 16 are powers of 2 because their binary representations have only one bit set.
Understanding this concept is crucial for efficient programming and algorithm design.
π’ Optimizing the Program
The provided program is a straightforward implementation. However, there are more advanced techniques for checking if a number is a power of 2, such as using logarithmic operations or counting the number of set bits. Depending on your specific use case, you might explore these optimizations.
Feel free to incorporate and modify this code as needed for your specific requirements. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C# Program to Check Power of 2), please comment here. I will help you immediately.