Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples Last Updated : 28 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 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 : Java // 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 // 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 Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#isPowerOfTwo-long- Comment More infoAdvertise with us Next Article Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples S Sahil_Bansall Follow Improve Article Tags : Java Java-Functions java-guava Guava-LongMath Practice Tags : Java Similar Reads Java Guava | pow(long b, int k) of LongMath Class with Examples The method pow(long b, int k) of Guava's LongMath Class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).longValue(). This implementation runs in O(log k) time. Syntax: public static long pow(long b, int k) Parameters: The method accepts two 2 min read Java Guava | mean(long x, long y) of LongMath Class with Examples The mean(long x, long y) method of Guava's LongMath Class accepts two parameters x and y and calculates arithmetic mean of them rounded towards negative infinity. This method is overflow resilient. Syntax: public static long mean(long x, long y) Parameters: This method accepts two parameters x and y 2 min read Java Guava | mod(long x, long m) of LongMath Class with Examples The mod(long x, long m) method of Guava's LongMath Class accepts two parameters x and m, and used to calculate the value of x modulus under m. Syntax: public static long mod(long x, long m) Parameters: This method accepts two parameters x and m which are of long type to calculate x modulo m. Return 2 min read Java Guava | gcd(long a, long b) of LongMath Class with Examples The method gcd(long a, long b) of Guava's LongMath Class returns the greatest common divisor of two parameters a and b. Syntax: public static long gcd(long a, long b) Parameters: This method accepts two parameters a and b of the long type of whose GCD is to be found. Return Type: This method returns 2 min read Java Guava | binomial(int n, int k) of LongMath Class with Examples The binomial(int n, int k) method of Guava's LongMath Class accepts two parameters n and k and calculate the value of the binomial coefficient {n}\choose{k}. If the calculated value overflows the maximum value of a long, then the method returns Long.MAX_VALUE i.e, the maximum value of a long. Syntax 2 min read Like