check if power of 2 java code example
Example 1: java is power of 2
boolean result = number > 0 && ((number & (number - 1)) == 0);
Example 2: how to check if a number is a power of 2 java
//create a boolean method that checks if number is a power of 2 by using modulus
public static boolean powerOfTwoGeneral(int n)
{
while(n%2==0)
{
n=n/2;
}
if(n==1)
{
return true;
}
else
{
return false;
}
}