Given an integer, check if it's the power of two.
class Main {
static class Solution {
private final boolean answer;
Solution(int num) {
if (num <= 0) {
answer = false;
}
else if ((num & (num - 1)) == 0) {
answer = true;
}
else {
answer = false;
}
}
public boolean isPowerOfTwo() {
return answer;
}
}
public static void runTests() {
// Falsy
System.out.println(new Solution(-1).isPowerOfTwo());
System.out.println(new Solution(0).isPowerOfTwo());
System.out.println(new Solution(Integer.MAX_VALUE).isPowerOfTwo());
System.out.println(new Solution(Integer.MIN_VALUE).isPowerOfTwo());
//Truthy
System.out.println(new Solution(1).isPowerOfTwo());
System.out.println(new Solution(128).isPowerOfTwo());
System.out.println(new Solution(1024).isPowerOfTwo());
}
public static void main(String[] args) {
runTests();
}
}