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.

**Syntax:**

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

*if x doesn’t represent power of 2.*

**false****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 :**

`// Java code to show implementation of` `// isPowerOfTwo(long x) method of Guava's` `// LongMath class` ` ` `import` `java.math.RoundingMode;` `import` `com.google.common.math.LongMath;` ` ` `class` `GFG {` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `long` `n1 = ` `52` `;` ` ` ` ` `// Using isPowerOfTwo(long x) method` ` ` `// of Guava's LongMath class` ` ` `if` `(LongMath.isPowerOfTwo(n1))` ` ` `System.out.println(n1` ` ` `+ ` `" is power of 2"` `);` ` ` `else` ` ` `System.out.println(n1` ` ` `+ ` `" is not power of 2"` `);` ` ` ` ` `long` `n2 = ` `4` `;` ` ` ` ` `// Using isPowerOfTwo(long x) method` ` ` `// of Guava's LongMath class` ` ` `if` `(LongMath.isPowerOfTwo(n2))` ` ` `System.out.println(n2` ` ` `+ ` `" is power of 2"` `);` ` ` `else` ` ` `System.out.println(n2` ` ` `+ ` `" is not power of 2"` `);` ` ` `}` `}` |

**Output:**

52 is not power of 2 4 is power of 2

**Example 2:**

`// Java code to show implementation of` `// isPowerOfTwo(long x) method of Guava's` `// LongMath class` ` ` `import` `java.math.RoundingMode;` `import` `com.google.common.math.LongMath;` ` ` `class` `GFG {` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `long` `n1 = ` `256` `;` ` ` ` ` `// Using isPowerOfTwo(long x) method` ` ` `// of Guava's LongMath class` ` ` `if` `(LongMath.isPowerOfTwo(n1))` ` ` `System.out.println(n1` ` ` `+ ` `" is power of 2"` `);` ` ` `else` ` ` `System.out.println(n1` ` ` `+ ` `" is not power of 2"` `);` ` ` ` ` `long` `n2 = ` `4096` `;` ` ` ` ` `// Using isPowerOfTwo(long x) method` ` ` `// of Guava's LongMath class` ` ` `if` `(LongMath.isPowerOfTwo(n2))` ` ` `System.out.println(n2` ` ` `+ ` `" is power of 2"` `);` ` ` `else` ` ` `System.out.println(n2` ` ` `+ ` `" is not power of 2"` `);` ` ` `}` `}` |

**Output:**

256 is power of 2 4096 is power of 2

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****.**