ユーザーが入力した数値が 2 の累乗かどうかを調べたい。
コードが機能しません。
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");
}
}
}
二乗の求め方を教えてください。
たとえば、8 は 2 の累乗です。22は 2 の累乗で
はありません。