Top Description Fields Constructors Methods
com.google.common.math

public final Class IntMath

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.math.MathPreconditions.checkNoOverflow, .MathPreconditions.checkNonNegative, .MathPreconditions.checkPositive, .MathPreconditions.checkRoundingUnnecessary, java.lang.Math.abs, .Math.min, java.math.RoundingMode.HALF_EVEN, .RoundingMode.HALF_UP

A class for arithmetic on values of type int. 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 long and for BigInteger can be found in LongMath and BigIntegerMath respectively. For other common operations on int values, see com.google.common.primitives.Ints.

Author
Louis Wasserman
Since
11.0

Field Summary

Modifier and TypeField and Description
pack-priv static int[]
private static final int[]
pack-priv static final int
pack-priv static final int[]
pack-priv static final int
MAX_POWER_OF_SQRT2_UNSIGNED

The biggest half power of two that can fit in an unsigned int.

pack-priv static final int
pack-priv static final byte[]
pack-priv static final int[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static int
binomial(int n, int k)

Returns n choose k, also known as the binomial coefficient of n and k, or Integer#MAX_VALUE if the result does not fit in an int.

public static int
ceilingPowerOfTwo(int x)

Returns the smallest power of two greater than or equal to x.

public static int
checkedAdd(int a, int b)

Returns the sum of a and b, provided it does not overflow.

public static int
checkedMultiply(int a, int b)

Returns the product of a and b, provided it does not overflow.

public static int
checkedPow(int b, int k)

Returns the b to the kth power, provided it does not overflow.

public static int
checkedSubtract(int a, int b)

Returns the difference of a and b, provided it does not overflow.

public static int
divide(int p, int q, RoundingMode mode)

Returns the result of dividing p by q, rounding using the specified RoundingMode.

public static int
factorial(int n)

Returns n!

public static int
floorPowerOfTwo(int x)

Returns the largest power of two less than or equal to x.

public static int
gcd(int a, int b)

Returns the greatest common divisor of a, b.

public static boolean
isPowerOfTwo(int x)

Returns true if x represents a power of two.

public static boolean
isPrime(int n)

Returns true if n is a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers.

pack-priv static int
lessThanBranchFree(int x, int y)

Returns 1 if x < y as unsigned integers, and 0 otherwise.

public static int
log10(int x, RoundingMode mode)

Returns the base-10 logarithm of x, rounded according to the specified rounding mode.

private static int
log10Floor(int x)

public static int
log2(int x, RoundingMode mode)

Returns the base-2 logarithm of x, rounded according to the specified rounding mode.

public static int
mean(int x, int y)

Returns the arithmetic mean of x and y, rounded towards negative infinity.

public static int
mod(int x, int m)

Returns x mod m, a non-negative value less than m.

public static int
pow(int b, int k)

Returns b to the kth power.

public static int
saturatedAdd(int a, int b)

Returns the sum of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

public static int
saturatedMultiply(int a, int b)

Returns the product of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

public static int
saturatedPow(int b, int k)

Returns the b to the kth power, unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

public static int
saturatedSubtract(int a, int b)

Returns the difference of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

public static int
sqrt(int x, RoundingMode mode)

Returns the square root of x, rounded with the specified rounding mode.

private static int
sqrtFloor(int x)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

biggestBinomialsback to summary
pack-priv static int[] biggestBinomials
Annotations
@VisibleForTesting
factorialsback to summary
private static final int[] factorials
FLOOR_SQRT_MAX_INTback to summary
pack-priv static final int FLOOR_SQRT_MAX_INT
Annotations
@VisibleForTesting
halfPowersOf10back to summary
pack-priv static final int[] halfPowersOf10
Annotations
@VisibleForTesting
MAX_POWER_OF_SQRT2_UNSIGNEDback to summary
pack-priv static final int MAX_POWER_OF_SQRT2_UNSIGNED

The biggest half power of two that can fit in an unsigned int.

Annotations
@VisibleForTesting
MAX_SIGNED_POWER_OF_TWOback to summary
pack-priv static final int MAX_SIGNED_POWER_OF_TWO
Annotations
@VisibleForTesting
maxLog10ForLeadingZerosback to summary
pack-priv static final byte[] maxLog10ForLeadingZeros
Annotations
@VisibleForTesting
powersOf10back to summary
pack-priv static final int[] powersOf10
Annotations
@VisibleForTesting

Constructor Detail

IntMathback to summary
private IntMath()

Method Detail

binomialback to summary
public static int binomial(int n, int k)

Returns n choose k, also known as the binomial coefficient of n and k, or Integer#MAX_VALUE if the result does not fit in an int.

Exceptions
IllegalArgumentException:
if n < 0, k < 0 or k > n
ceilingPowerOfTwoback to summary
public static int ceilingPowerOfTwo(int x)

Returns the smallest power of two greater than or equal to x. This is equivalent to checkedPow(2, log2(x, CEILING)).

Exceptions
IllegalArgumentException:
if x <= 0
ArithmeticException:
of the next-higher power of two is not representable as an int, i.e. when x > 2^30
Since
20.0
checkedAddback to summary
public static int checkedAdd(int a, int b)

Returns the sum of a and b, provided it does not overflow.

Exceptions
ArithmeticException:
if a + b overflows in signed int arithmetic
checkedMultiplyback to summary
public static int checkedMultiply(int a, int b)

Returns the product of a and b, provided it does not overflow.

Exceptions
ArithmeticException:
if a * b overflows in signed int arithmetic
checkedPowback to summary
public static int checkedPow(int b, int k)

Returns the b to the kth power, provided it does not overflow.

pow may be faster, but does not check for overflow.

Exceptions
ArithmeticException:
if b to the kth power overflows in signed int arithmetic
checkedSubtractback to summary
public static int checkedSubtract(int a, int b)

Returns the difference of a and b, provided it does not overflow.

Exceptions
ArithmeticException:
if a - b overflows in signed int arithmetic
divideback to summary
public static int divide(int p, int q, RoundingMode mode)

Returns the result of dividing p by q, rounding using the specified RoundingMode.

Annotations
@SuppressWarnings:fallthrough
Exceptions
ArithmeticException:
if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b
factorialback to summary
public static int factorial(int n)

Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Integer#MAX_VALUE if the result does not fit in a int.

Exceptions
IllegalArgumentException:
if n < 0
floorPowerOfTwoback to summary
public static int floorPowerOfTwo(int x)

Returns the largest power of two less than or equal to x. This is equivalent to checkedPow(2, log2(x, FLOOR)).

Exceptions
IllegalArgumentException:
if x <= 0
Since
20.0
gcdback to summary
public static int gcd(int a, int b)

Returns the greatest common divisor of a, b. Returns 0 if a == 0 && b == 0.

Exceptions
IllegalArgumentException:
if a < 0 or b < 0
isPowerOfTwoback to summary
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.

isPrimeback to summary
public static boolean isPrime(int n)

Returns true if n is a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers. Returns false if n is zero, one, or a composite number (one which can be factored into smaller positive integers).

To test larger numbers, use LongMath#isPrime or BigInteger#isProbablePrime.

Annotations
@J2ktIncompatible
@GwtIncompatible
Exceptions
IllegalArgumentException:
if n is negative
Since
20.0
lessThanBranchFreeback to summary
pack-priv static int lessThanBranchFree(int x, int y)

Returns 1 if x < y as unsigned integers, and 0 otherwise. Assumes that x - y fits into a signed int. The implementation is branch-free, and benchmarks suggest it is measurably (if narrowly) faster than the straightforward ternary expression.

Annotations
@VisibleForTesting
log10back to summary
public static int log10(int x, RoundingMode mode)

Returns the base-10 logarithm of x, rounded according to the specified rounding mode.

Annotations
@J2ktIncompatible
@GwtIncompatible
@SuppressWarnings:fallthrough
Exceptions
IllegalArgumentException:
if x <= 0
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and x is not a power of ten
log10Floorback to summary
private static int log10Floor(int x)
log2back to summary
public static int log2(int x, RoundingMode mode)

Returns the base-2 logarithm of x, rounded according to the specified rounding mode.

Annotations
@SuppressWarnings:fallthrough
Exceptions
IllegalArgumentException:
if x <= 0
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and x is not a power of two
meanback to summary
public static int mean(int x, int y)

Returns the arithmetic mean of x and y, rounded towards negative infinity. This method is overflow resilient.

Since
14.0
modback to summary
public static int mod(int x, int m)

Returns 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) == 0
Exceptions
ArithmeticException:
if m <= 0
See Also
Remainder Operator
powback to summary
public static int pow(int b, int k)

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.

Compare checkedPow, which throws an ArithmeticException upon overflow.

Annotations
@J2ktIncompatible
@GwtIncompatible
Exceptions
IllegalArgumentException:
if k < 0
saturatedAddback to summary
public static int saturatedAdd(int a, int b)

Returns the sum of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

Since
20.0
saturatedMultiplyback to summary
public static int saturatedMultiply(int a, int b)

Returns the product of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

Since
20.0
saturatedPowback to summary
public static int saturatedPow(int b, int k)

Returns the b to the kth power, unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

Since
20.0
saturatedSubtractback to summary
public static int saturatedSubtract(int a, int b)

Returns the difference of a and b unless it would overflow or underflow in which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.

Since
20.0
sqrtback to summary
public static int sqrt(int x, RoundingMode mode)

Returns the square root of x, rounded with the specified rounding mode.

Annotations
@GwtIncompatible
@SuppressWarnings:fallthrough
Exceptions
IllegalArgumentException:
if x < 0
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and sqrt(x) is not an integer
sqrtFloorback to summary
private static int sqrtFloor(int x)