I want to find if a user entered number is a power of two or not.
My code doesn't work.
public class power_of_two
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number : ");
int num = in.nextInt();
int other = 1;
if(((~num) & 1) == 1)
{
System.out.println("The number is a power of two");
}
else
{
System.out.println("The number is a NOT A power of two");
}
}
}
Let me know how can I find the power of two number.
For example 8 is a power of 2.
22 is not a power of 2, etc..