in commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java [279:318]
public static int pow(final int k,
final int e) {
if (e < 0) {
throw new IllegalArgumentException(NEGATIVE_EXPONENT_1 + e + NEGATIVE_EXPONENT_2);
}
if (k == 0) {
return e == 0 ? 1 : 0;
}
if (k == 1) {
return 1;
}
if (k == -1) {
return (e & 1) == 0 ? 1 : -1;
}
if (e >= 31) {
throw new ArithmeticException("integer overflow");
}
int exp = e;
int result = 1;
int k2p = k;
while (true) {
if ((exp & 0x1) != 0) {
result = Math.multiplyExact(result, k2p);
}
exp >>= 1;
if (exp == 0) {
break;
}
k2p = Math.multiplyExact(k2p, k2p);
}
return result;
}