Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples
The method isPowerOfTwo(long x) of Guava’s LongMath Class is used to check if a number is power of two or not. It accepts the number to be checked as a parameter and return boolean value true or false based on whether the number is a power of 2 or not.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public static boolean isPowerOfTwo(long x)
Parameter: This method accepts a single parameter x which is of long type which is to be checked for power of two.
Return Value: This method returns a boolean value. It returns true if x represents power of 2 and false if x doesn’t represent power of 2.
Exceptions: The method doesn’t throw any exception.
Note: This differs from Long.bitCount(x) == 1, because Long.bitCount(Long.MIN_VALUE) == 1, but Long.MIN_VALUE is not a power of two.
Example 1 :
52 is not power of 2 4 is power of 2
256 is power of 2 4096 is power of 2