Open In App

PHP Program to find whether a no is power of two

Last Updated : 02 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer, write a function to find if it is a power of two or not.

Examples :

Input : n = 4
Output : Yes
22 = 4

Input : n = 7
Output : No

Input : n = 32
Output : Yes
25 = 32

1. A simple method for this is to simply take the log of the number on base 2 and if you get an integer then number is power of 2.

PHP




<?php
// PHP Program to find 
// whether a no is 
// power of two
  
// Function to check
// Log base 2
function Log2($x)
{
    return (log10($x) / 
            log10(2));
}
  
  
// Function to check
// if x is power of 2
function isPowerOfTwo($n)
{
    return (ceil(Log2($n)) == 
            floor(Log2($n)));
}
  
// Driver Code
if(isPowerOfTwo(31))
echo "Yes\n";
else
echo "No\n";
  
if(isPowerOfTwo(64))
echo "Yes\n";
else
echo "No\n";
      
// This code is contributed 
// by Sam007
?>


Output:

No
Yes

2. Another solution is to keep dividing the number by two, i.e, do n = n/2 iteratively. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.

PHP




<?php
  
// Function to check if
// x is power of 2
function isPowerOfTwo($n)
{
if ($n == 0)
    return 0;
while ($n != 1)
{
    if ($n % 2 != 0)
        return 0;
    $n = $n / 2;
}
return 1;
}
  
// Driver Code
if(isPowerOfTwo(31))
    echo "Yes\n";
else
    echo "No\n";
  
if(isPowerOfTwo(64))
    echo "Yes\n";
else
    echo "No\n";
  
// This code is contributed 
// by Sam007
?>


Output:

No
Yes

3. All power of two numbers have only one bit set. So count the no. of set bits and if you get 1 then number is a power of 2. Please see Count set bits in an integer for counting set bits.

4. If we subtract a power of 2 numbers by 1 then all unset bits after the only set bit become set; and the set bit become unset.

For example for 4 ( 100) and 16(10000), we get following after subtracting 1
3 –> 011
15 –> 01111

So, if a number n is a power of 2 then bitwise & of n and n-1 will be zero. We can say n is a power of 2 or not based on value of n&(n-1). The expression n&(n-1) will not work when n is 0. To handle this case also, our expression will become n& (!n&(n-1)) (thanks to https://www.geeksforgeeks.org/program-to-find-whether-a-no-is-power-of-two/Mohammad for adding this case).
Below is the implementation of this method.

PHP




<?php
// PHP program to efficiently 
// check for power for 2
  
// Function to check if
// x is power of 2
function isPowerOfTwo ($x)
{
// First x in the below expression
// is for the case when x is 0 
return $x && (!($x & ($x - 1)));
}
  
// Driver Code
if(isPowerOfTwo(31))
    echo "Yes\n" ;
else
    echo "No\n";
  
if(isPowerOfTwo(64))
    echo "Yes\n" ;
else
    echo "No\n";
          
// This code is contributed by Sam007
?>


Output:

No
Yes

Please refer complete article on Program to find whether a no is power of two for more details!



Similar Reads

JavaScript program to check whether a given number is power of 2
Given a positive integer n, write a function to find if it is a power of 2 or not Examples: Input: n = 4 Output: Yes Explanation: 22 = 4 Input: n = 32 Output: Yes Explanation: 25 = 32 To solve the problem follow the below idea: A simple method for this is to simply take the log of the number on base 2 and if you get an integer then the number is th
6 min read
JavaScript Program to Find the Next Power of Two
Given a positive integer, find the next power of two greater than the given number using JavaScript. Examples: Input: N = 6Output: 8Explanation: The next power of two greater than 6 is 8.Input: N = 16Output: 16Explanation: 16 is already a power of two, so the next power of two is also 16.Below are the approaches to finding the Next Power of Two whi
3 min read
Check whether a given Number is Power-Isolated or not
Given a integer N, with prime factorisation n1p1 * n2p2 ...... The task is to check if the integer N is power-isolated or not. An integer is said to be power-isolated if n1 * p1 * n2 * p2 ..... = N. Examples: Input: N = 12Output: Power-isolated Integer.Input: N = 18Output: Not a power-isolated integer.Recommended: Please try your approach on {IDE}
12 min read
Php Program to Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
2 min read
Php Program for Pairs such that one is a power multiple of other
You are given an array A[] of n-elements and a positive integer k (k &gt; 1). Now you have find the number of pairs Ai, Aj such that Ai = Aj*(kx) where x is an integer. Note: (Ai, Aj) and (Aj, Ai) must be count once.Examples : Input : A[] = {3, 6, 4, 2}, k = 2 Output : 2 Explanation : We have only two pairs (4, 2) and (3, 6) Input : A[] = {2, 2, 2}
3 min read
Javascript Program To Check Whether Two Strings Are Anagram Of Each Other
Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an anagram of each other. We strongly recommend that you click here and practice it, before moving on to t
7 min read
JavaScript Program to check Whether a Number can be Express as Sum of Two Prime Numbers
To check if a number can be expressed as the sum of two prime numbers, iterate through primes less than or equal to half of the given number. For each prime, check if the complementary prime exists by subtracting it from the target number and testing if the result is prime. If found, the number can be expressed as the sum of two primes. There are s
3 min read
Program to find the number of days between two dates in PHP
In this article, we will see how to get the date difference in the number of days in PHP, along with will also understand the various ways to get the total count of difference in 2 dates &amp; see their implementation through the examples. We have given two dates &amp; our task is to find the number of days between these given dates. For this, we w
3 min read
PHP Program to Find the closest pair from two sorted arrays
Given two sorted arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array. We are given two arrays ar1[0...m-1] and ar2[0..n-1] and a number x, we need to find the pair ar1[i] + ar2[j] such that absolute value of (ar1[i] + ar2[j] - x) is minimum.Example: Input: ar1[] = {1, 4, 5, 7}; ar2[] = {10, 20,
2 min read
PHP Program to Find GCD or HCF of Two Numbers
Given two numbers, the task is to find the GCD or HCF of two numbers in PHP. GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. Formula to Find GCDGCD ( a, b ) = [ |a.b| ] / [ lcm(a, b) ]HCF of factors = Product of the Numbers/ LCM of num
2 min read