// 2的次冪 public static int check(int num) { if (num % 2 == 0) { int count = num / 2; if (count > 0 && count != 1) { count = check(count); } return count; }else{ return 0; } } // 等於1即成立 public static void main(String[] args) {
System.out.println(check(129) == 1 ?true :false);
}
第二種方法(二進制更簡單)
public static boolean check(int num) {
return num & (num - 1);
}