Class MathUtils

java.lang.Object
org.eclipse.jetty.util.MathUtils

public class MathUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    cappedAdd(int a, int b, int maxValue)
    Returns the sum of its arguments, capping to maxValue.
    static long
    cappedAdd(long a, long b)
    Returns the sum of its arguments, capping to Long.MAX_VALUE if they overflow.
    static int
    ceilLog2(int value)
    Computes binary logarithm of the given number ceiled to the next power of two.
    static int
    Get the next highest power of two
    static boolean
    sumOverflows(int a, int b)
    Returns whether the sum of the arguments overflows an int.

    Methods inherited from class java.lang.Object

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

    • sumOverflows

      public static boolean sumOverflows(int a, int b)
      Returns whether the sum of the arguments overflows an int.
      Parameters:
      a - the first value
      b - the second value
      Returns:
      whether the sum of the arguments overflows an int
    • cappedAdd

      public static long cappedAdd(long a, long b)
      Returns the sum of its arguments, capping to Long.MAX_VALUE if they overflow.
      Parameters:
      a - the first value
      b - the second value
      Returns:
      the sum of the values, capped to Long.MAX_VALUE
    • cappedAdd

      public static int cappedAdd(int a, int b, int maxValue)
      Returns the sum of its arguments, capping to maxValue.
      Parameters:
      a - the first value
      b - the second value
      Returns:
      the sum of the values, capped to maxValue
    • ceilToNextPowerOfTwo

      public static int ceilToNextPowerOfTwo(int value)
      Get the next highest power of two
      Parameters:
      value - An integer
      Returns:
      a power of two that is greater than or equal to value
    • ceilLog2

      public static int ceilLog2(int value)
      Computes binary logarithm of the given number ceiled to the next power of two. If the given number is 800, it is ceiled to 1024 which is the closest power of 2 greater than or equal to 800, then 1024 is 10_000_000_000 in binary, i.e.: 1 followed by 10 zeros, and it's also 2 to the power of 10. So for the numbers between 513 and 1024 the returned value is 10.
      Parameters:
      value - An integer
      Returns:
      the binary logarithm of the given number ceiled to the next power of two.