Java Guava | isPowerOfTwo() method IntMath Class Last Updated : 23 Jan, 2019 Comments Improve Suggest changes Like Article Like Report The isPowerOfTwo() method of Guava's IntMath 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(int x) Parameter: This method accepts a single parameter x which of integer type. Return Value : The method returns true if x represents power of 2 and false if x doesn't represent power of 2. Exceptions : The method doesn't have any exception. Note : This differs from Integer.bitCount(x) == 1, because Integer.bitCount(Integer.MIN_VALUE) == 1, but Integer.MIN_VALUE is not a power of two. Example 1 : Java // Java code to show implementation of // isPowerOfTwo(int x) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { // Driver code public static void main(String args[]) { int a1 = 63; // Using isPowerOfTwo(int x) method // of Guava's IntMath class if (IntMath.isPowerOfTwo(a1)) System.out.println(a1 + " is power of 2"); else System.out.println(a1 + " is not power of 2"); int a2 = 1024; // Using isPowerOfTwo(int x) method // of Guava's IntMath class if (IntMath.isPowerOfTwo(a2)) System.out.println(a2 + " is power of 2"); else System.out.println(a2 + " is not power of 2"); } } Output : 63 is not power of 2 1024 is power of 2 Example 2 : Java // Java code to show implementation of // isPowerOfTwo(int x) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { // Driver code public static void main(String args[]) { int a1 = 10; // Using isPowerOfTwo(int x) method // of Guava's IntMath class if (IntMath.isPowerOfTwo(a1)) System.out.println(a1 + " is power of 2"); else System.out.println(a1 + " is not power of 2"); int a2 = 256; // Using isPowerOfTwo(int x) method // of Guava's IntMath class if (IntMath.isPowerOfTwo(a2)) System.out.println(a2 + " is power of 2"); else System.out.println(a2 + " is not power of 2"); } } Output : 10 is not power of 2 256 is power of 2 Reference : https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#isPowerOfTwo-int- Comment More infoAdvertise with us Next Article Java Guava | isPowerOfTwo() method IntMath Class B bansal_rtk_ Follow Improve Article Tags : Misc Java Java-Functions java-guava Practice Tags : JavaMisc Similar Reads Java Guava | isPrime() method of IntMath Class The isPrime(int n) method of Guava's IntMath class is used to check whether the parameter passed to it is a prime number or not. If the parameter passed to it is prime, then it returns True otherwise it returns False. A number is said to be Prime if it is divisible only by 1 and the number itself. S 2 min read IntMath Class | Guava | Java Introduction : IntMath is used to perform mathematical operations on Integer values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports onl 3 min read Java Guava | binomial() method of IntMath Class The binomial(int n, int k) method of Guava's IntMath 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 an integer than the method returns Integer.MAX_VALUE or in other words the maximum value 2 min read Java Guava | floorPowerOfTwo() method IntMath Class The floorPowerOfTwo() method of Guava's IntMath class accepts a parameter and returns the largest power of two less than the value passed in the parameter. This is equivalent to: checkedPow(2, log2(x, FLOOR)). Syntax : public static int floorPowerOfTwo(int x) Parameters: This method accepts a single 2 min read Java Guava | pow(int b, int k) method of IntMath Class The method pow(int b, int k) of Guava's IntMath class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).intValue(). This implementation runs in O(log k) time. Syntax: public static int pow(int b, int k) Exception: The method pow(int b, int k) 2 min read Like