PHP Basic
PHP Interview Programs
- PHP Interview Programs
- PHP Abundant Number
- PHP Amicable Number
- PHP Armstrong Number
- PHP Average of N Numbers
- PHP Automorphic Number
- PHP Biggest of three numbers
- PHP Binary to Decimal
- PHP Common Divisors
- PHP Composite Number
- PHP Condense a Number
- PHP Cube Number
- PHP Decimal to Binary
- PHP Decimal to Octal
- PHP Disarium Number
- PHP Even Number
- PHP Evil Number
- PHP Factorial of a Number
- PHP Fibonacci Series
- PHP GCD
- PHP Happy Number
- PHP Harshad Number
- PHP LCM
- PHP Leap Year
- PHP Magic Number
- PHP Matrix Addition
- PHP Matrix Division
- PHP Matrix Multiplication
- PHP Matrix Subtraction
- PHP Matrix Transpose
- PHP Maximum Value of an Array
- PHP Minimum Value of an Array
- PHP Multiplication Table
- PHP Natural Number
- PHP Number Combination
- PHP Odd Number
- PHP Palindrome Number
- PHP Pascalβs Triangle
- PHP Perfect Number
- PHP Perfect Square
- PHP Power of 2
- PHP Power of 3
- PHP Pronic Number
- PHP Prime Factor
- PHP Prime Number
- PHP Smith Number
- PHP Strong Number
- PHP Sum of Array
- PHP Sum of Digits
- PHP Swap Two Numbers
- PHP Triangular Number
PHP 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 PHP program that efficiently checks whether a given number is a power of 2.
π Example
Let's take a look at the PHP code that accomplishes this functionality.
<?php
// Function to check if a number is a power of 2
function isPowerOfTwo($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
// Replace this value with your desired number
$number = 16;
// Check if the number is a power of 2
if (isPowerOfTwo($number))
{
echo "$number is a power of 2.\n";
}
else
{
echo "$number is not a power of 2.\n";
}
?>
π» Testing the Program
To test the program with different numbers, simply replace the value of $number in the code.
16 is a power of 2.
Run the script 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, a characteristic of power-of-2 numbers.
- The program demonstrates the usage of the isPowerOfTwo function by checking if a given number is a power of 2 in the main program.
π 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.
<?php
// Function to check if a number is a power of 2
function isPowerOf2($num)
{
return ($num & ($num - 1)) == 0;
}
// Display powers of 2 in the range 1 to 20
echo "Power of 2 in the range 1 to 20:\n";
for ($i = 1;$i <= 20;++$i)
{
if (isPowerOf2($i))
{
echo $i . " ";
}
}
?>
π» Testing the Program
Power of 2 in the range 1 to 20: 1 2 4 8 16
Simply run the script, and it will display the powers of 2 in the range from 1 to 20.
π§ How the Program Works
- The program defines a function isPowerOf2 that checks whether a given number is a power of 2 using a bitwise operation.
- In the main part of the program, it iterates through numbers from 1 to 20.
- For each number, it checks if it's a power of 2 using the isPowerOf2 function.
- If the number is a power of 2, it is displayed.
π§ 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 (PHP Program to Check Power of 2), please comment here. I will help you immediately.