@GwtCompatible(emulated=true) public final class LongMath extends Object
long. Where possible, methods are defined and
 named analogously to their BigInteger counterparts.
 The implementations of many methods in this class are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
Similar functionality for int and for BigInteger can be found in
 IntMath and BigIntegerMath respectively.  For other common operations on
 long values, see Longs.
| Modifier and Type | Method and Description | 
|---|---|
| static long | binomial(int n,
                int k)Returns  nchoosek, also known as the binomial coefficient ofnandk, orLong.MAX_VALUEif the result does not fit in along. | 
| static long | checkedAdd(long a,
                    long b)Returns the sum of  aandb, provided it does not overflow. | 
| static long | checkedMultiply(long a,
                              long b)Returns the product of  aandb, provided it does not overflow. | 
| static long | checkedPow(long b,
                    int k)Returns the  bto thekth power, provided it does not overflow. | 
| static long | checkedSubtract(long a,
                              long b)Returns the difference of  aandb, provided it does not overflow. | 
| static long | divide(long p,
            long q,
            RoundingMode mode)Returns the result of dividing  pbyq, rounding using the specifiedRoundingMode. | 
| static long | factorial(int n)Returns  n!, that is, the product of the firstnpositive
 integers,1ifn == 0, orLong.MAX_VALUEif the
 result does not fit in along. | 
| static long | gcd(long a,
      long b)Returns the greatest common divisor of  a, b. | 
| static boolean | isPowerOfTwo(long x)Returns  trueifxrepresents a power of two. | 
| static int | log10(long x,
          RoundingMode mode)Returns the base-10 logarithm of  x, rounded according to the specified rounding mode. | 
| static int | log2(long x,
        RoundingMode mode)Returns the base-2 logarithm of  x, rounded according to the specified rounding mode. | 
| static long | mean(long x,
        long y)Returns the arithmetic mean of  xandy, rounded toward
 negative infinity. | 
| static int | mod(long x,
      int m)Returns  x mod m, a non-negative value less thanm. | 
| static long | mod(long x,
      long m)Returns  x mod m, a non-negative value less thanm. | 
| static long | pow(long b,
      int k)Returns  bto thekth power. | 
| static long | sqrt(long x,
        RoundingMode mode)Returns the square root of  x, rounded with the specified rounding mode. | 
public static boolean isPowerOfTwo(long x)
true if x represents a power of two.
 This differs from Long.bitCount(x) == 1, because
 Long.bitCount(Long.MIN_VALUE) == 1, but Long.MIN_VALUE is not a power of two.
public static int log2(long x, RoundingMode mode)
x, rounded according to the specified rounding mode.IllegalArgumentException - if x <= 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
         is not a power of two@GwtIncompatible(value="TODO") public static int log10(long x, RoundingMode mode)
x, rounded according to the specified rounding mode.IllegalArgumentException - if x <= 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
         is not a power of ten@GwtIncompatible(value="TODO") public static long pow(long b, int k)
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.IllegalArgumentException - if k < 0@GwtIncompatible(value="TODO") public static long sqrt(long x, RoundingMode mode)
x, rounded with the specified rounding mode.IllegalArgumentException - if x < 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and
         sqrt(x) is not an integer@GwtIncompatible(value="TODO") public static long divide(long p, long q, RoundingMode mode)
p by q, rounding using the specified
 RoundingMode.ArithmeticException - if q == 0, or if mode == UNNECESSARY and a
         is not an integer multiple of b@GwtIncompatible(value="TODO") public static int mod(long x, int m)
x mod m, a non-negative value less than m.
 This differs from x % m, which might be negative.
 For example:
 mod(7, 4) == 3
 mod(-7, 4) == 1
 mod(-1, 4) == 3
 mod(-8, 4) == 0
 mod(8, 4) == 0ArithmeticException - if m <= 0@GwtIncompatible(value="TODO") public static long mod(long x, long m)
x mod m, a non-negative value less than m.
 This differs from x % m, which might be negative.
 For example:
 mod(7, 4) == 3
 mod(-7, 4) == 1
 mod(-1, 4) == 3
 mod(-8, 4) == 0
 mod(8, 4) == 0ArithmeticException - if m <= 0public static long gcd(long a, long b)
a, b. Returns 0 if
 a == 0 && b == 0.IllegalArgumentException - if a < 0 or b < 0@GwtIncompatible(value="TODO") public static long checkedAdd(long a, long b)
a and b, provided it does not overflow.ArithmeticException - if a + b overflows in signed long arithmetic@GwtIncompatible(value="TODO") public static long checkedSubtract(long a, long b)
a and b, provided it does not overflow.ArithmeticException - if a - b overflows in signed long arithmetic@GwtIncompatible(value="TODO") public static long checkedMultiply(long a, long b)
a and b, provided it does not overflow.ArithmeticException - if a * b overflows in signed long arithmetic@GwtIncompatible(value="TODO") public static long checkedPow(long b, int k)
b to the kth power, provided it does not overflow.ArithmeticException - if b to the kth power overflows in signed
         long arithmetic@GwtIncompatible(value="TODO") public static long factorial(int n)
n!, that is, the product of the first n positive
 integers, 1 if n == 0, or Long.MAX_VALUE if the
 result does not fit in a long.IllegalArgumentException - if n < 0public static long binomial(int n, int k)
n choose k, also known as the binomial coefficient of n and
 k, or Long.MAX_VALUE if the result does not fit in a long.IllegalArgumentException - if n < 0, k < 0, or k > npublic static long mean(long x, long y)
x and y, rounded toward
 negative infinity. This method is resilient to overflow.Copyright © 2010-2015. All Rights Reserved.