Implement the method isPowerOfTwo that checks whether an integer is a power of two.

A number is a power of two if it can be written as 2^k for some whole number k.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Return false for zero and negative numbers.
  • 1 is a power of two.
  • You may use division or bit operations.
Example 1
Input:
n (int) = 1
Return:
(boolean) true
Example 2
Input:
n (int) = 16
Return:
(boolean) true
Example 3
Input:
n (int) = 18
Return:
(boolean) false
Example 4
Input:
n (int) = 0
Return:
(boolean) false

Repeatedly divide by two while the number is even. The final value must be one.

Run your code to see the result.