Class MathUtils


  • public final class MathUtils
    extends java.lang.Object
    Methods to perform fast log
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isPowerOfTwo​(int x)
      Returns true if x represents a power of two.
      static int log2​(int x, java.math.RoundingMode mode)
      Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • log2

        public static int log2​(int x,
                               java.math.RoundingMode mode)
        Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
        Throws:
        java.lang.IllegalArgumentException - if x <= 0
        java.lang.ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not a power of two
      • isPowerOfTwo

        public static boolean isPowerOfTwo​(int x)
        Returns true if x represents a power of two.

        This differs from Integer.bitCount(x) == 1, because Integer.bitCount(Integer.MIN_VALUE) == 1, but Integer.MIN_VALUE is not a power of two.