Class IntMath
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 Ints.
- Since:
- 11.0
- Author:
- Louis Wasserman
- 
Method SummaryModifier and TypeMethodDescriptionstatic intbinomial(int n, int k) Returnsnchoosek, also known as the binomial coefficient ofnandk, orInteger.MAX_VALUEif the result does not fit in anint.static intceilingPowerOfTwo(int x) Returns the smallest power of two greater than or equal tox.static intcheckedAdd(int a, int b) Returns the sum ofaandb, provided it does not overflow.static intcheckedMultiply(int a, int b) Returns the product ofaandb, provided it does not overflow.static intcheckedPow(int b, int k) Returns thebto thekth power, provided it does not overflow.static intcheckedSubtract(int a, int b) Returns the difference ofaandb, provided it does not overflow.static intdivide(int p, int q, RoundingMode mode) Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.static intfactorial(int n) Returnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orInteger.MAX_VALUEif the result does not fit in aint.static intfloorPowerOfTwo(int x) Returns the largest power of two less than or equal tox.static intgcd(int a, int b) Returns the greatest common divisor ofa, b.static booleanisPowerOfTwo(int x) Returnstrueifxrepresents a power of two.static booleanisPrime(int n) Returnstrueifnis a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers.static intlog10(int x, RoundingMode mode) Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.static intlog2(int x, RoundingMode mode) Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.static intmean(int x, int y) Returns the arithmetic mean ofxandy, rounded towards negative infinity.static intmod(int x, int m) Returnsx mod m, a non-negative value less thanm.static intpow(int b, int k) Returnsbto thekth power.static intsaturatedAbs(int x) Returns the closest representableintto the absolute value ofx.static intsaturatedAdd(int a, int b) Returns the sum ofaandbunless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.static intsaturatedMultiply(int a, int b) Returns the product ofaandbunless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.static intsaturatedPow(int b, int k) Returns thebto thekth power, unless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.static intsaturatedSubtract(int a, int b) Returns the difference ofaandbunless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.static intsqrt(int x, RoundingMode mode) Returns the square root ofx, rounded with the specified rounding mode.
- 
Method Details- 
ceilingPowerOfTwopublic static int ceilingPowerOfTwo(int x) Returns the smallest power of two greater than or equal tox. This is equivalent tocheckedPow(2, log2(x, CEILING)).- Throws:
- 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
 
- 
floorPowerOfTwopublic static int floorPowerOfTwo(int x) Returns the largest power of two less than or equal tox. This is equivalent tocheckedPow(2, log2(x, FLOOR)).- Throws:
- IllegalArgumentException- if- x <= 0
- Since:
- 20.0
 
- 
isPowerOfTwopublic static boolean isPowerOfTwo(int x) Returnstrueifxrepresents a power of two.This differs from Integer.bitCount(x) == 1, becauseInteger.bitCount(Integer.MIN_VALUE) == 1, butInteger.MIN_VALUEis not a power of two.
- 
log2Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.- Throws:
- IllegalArgumentException- if- x <= 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not a power of two
 
- 
log10Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.- Throws:
- IllegalArgumentException- if- x <= 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not a power of ten
 
- 
powReturnsbto thekth power. Even if the result overflows, it will be equal toBigInteger.valueOf(b).pow(k).intValue(). This implementation runs inO(log k)time.Compare checkedPow(int, int), which throws anArithmeticExceptionupon overflow.- Throws:
- IllegalArgumentException- if- k < 0
 
- 
sqrtReturns the square root ofx, rounded with the specified rounding mode.- Throws:
- IllegalArgumentException- if- x < 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- sqrt(x)is not an integer
 
- 
divideReturns the result of dividingpbyq, rounding using the specifiedRoundingMode. If theRoundingModeisRoundingMode.DOWN, then this method is equivalent to regular Java division,p / q; and if it isRoundingMode.FLOOR, then this method is equivalent toMath.floorDiv(p, q).- Throws:
- ArithmeticException- if- q == 0, or if- mode == UNNECESSARYand- ais not an integer multiple of- b
 
- 
modpublic static int mod(int x, int m) Returnsx mod m, a non-negative value less thanm. This differs fromx % m, which might be negative. This method is equivalent toMath.floorMod(x, m)except that that method also allows negativem.Math.floorModshould be preferred whenmis known to be positive.For example: mod(7, 4) == 3 mod(-7, 4) == 1 mod(-1, 4) == 3 mod(-8, 4) == 0 mod(8, 4) == 0- Throws:
- ArithmeticException- if- m <= 0
- See Also:
 
- 
gcdpublic static int gcd(int a, int b) Returns the greatest common divisor ofa, b. Returns0ifa == 0 && b == 0.- Throws:
- IllegalArgumentException- if- a < 0or- b < 0
 
- 
checkedAddReturns the sum ofaandb, provided it does not overflow.Note: this method is now unnecessary and should be treated as deprecated; use Math.addExact(int, int)instead.- Throws:
- ArithmeticException- if- a + boverflows in signed- intarithmetic
 
- 
checkedSubtractReturns the difference ofaandb, provided it does not overflow.Note: this method is now unnecessary and should be treated as deprecated; use Math.subtractExact(int, int)instead.- Throws:
- ArithmeticException- if- a - boverflows in signed- intarithmetic
 
- 
checkedMultiplyReturns the product ofaandb, provided it does not overflow.Note: this method is now unnecessary and should be treated as deprecated; use Math.multiplyExact(int, int)instead.- Throws:
- ArithmeticException- if- a * boverflows in signed- intarithmetic
 
- 
checkedPowpublic static int checkedPow(int b, int k) Returns thebto thekth power, provided it does not overflow.pow(int, int)may be faster, but does not check for overflow.- Throws:
- ArithmeticException- if- bto the- kth power overflows in signed- intarithmetic
 
- 
saturatedAddpublic static int saturatedAdd(int a, int b) Returns the sum ofaandbunless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
saturatedSubtractpublic static int saturatedSubtract(int a, int b) Returns the difference ofaandbunless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
saturatedMultiplypublic static int saturatedMultiply(int a, int b) Returns the product ofaandbunless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
saturatedPowpublic static int saturatedPow(int b, int k) Returns thebto thekth power, unless it would overflow or underflow in which caseInteger.MAX_VALUEorInteger.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
factorialpublic static int factorial(int n) Returnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orInteger.MAX_VALUEif the result does not fit in aint.- Throws:
- IllegalArgumentException- if- n < 0
 
- 
binomialpublic static int binomial(int n, int k) Returnsnchoosek, also known as the binomial coefficient ofnandk, orInteger.MAX_VALUEif the result does not fit in anint.- Throws:
- IllegalArgumentException- if- n < 0,- k < 0or- k > n
 
- 
meanpublic static int mean(int x, int y) Returns the arithmetic mean ofxandy, rounded towards negative infinity. This method is overflow resilient.- Since:
- 14.0
 
- 
isPrimeReturnstrueifnis a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers. Returnsfalseifnis zero, one, or a composite number (one which can be factored into smaller positive integers).To test larger numbers, use LongMath.isPrime(long)orBigInteger.isProbablePrime(int).- Throws:
- IllegalArgumentException- if- nis negative
- Since:
- 20.0
 
- 
saturatedAbspublic static int saturatedAbs(int x) Returns the closest representableintto the absolute value ofx.This is the same thing as the true absolute value of xexcept in the case whenxisInteger.MIN_VALUE, in which case this returnsInteger.MAX_VALUE. (Note thatInteger.MAX_VALUEis mathematically equal to-Integer.MIN_VALUE - 1.)There are three common APIs for determining the absolute value of an integer, all of which behave identically except when passed Integer.MIN_VALUE. Those methods are:- Math.abs(int), which returns- Integer.MIN_VALUEwhen passed- Integer.MIN_VALUE
- Math.absExact(int), which throws- ArithmeticExceptionwhen passed- Integer.MIN_VALUE
- this method, IntMath.saturatedAbs(int), which returnsInteger.MAX_VALUEwhen passedInteger.MIN_VALUE
 Note that if your only goal is to turn a well-distributed `int` (such as a random number or hash code) into a well-distributed nonnegative number, the most even distribution is achieved not by this method or other absolute value methods, but by x & Integer.MAX_VALUE.- Since:
- 33.5.0
 
 
-