guava/src/com/google/common/math/IntMath.java
public static int checkedPow(int b, int k) {
    checkNonNegative("exponent", k);
    switch (b) {
      case 0:
        return (k == 0) ? 1 : 0;
      case 1:
        return 1;
      case (-1):
        return ((k & 1) == 0) ? 1 : -1;
      case 2:
        checkNoOverflow(k < Integer.SIZE - 1, "checkedPow", b, k);
        return 1 << k;
      case (-2):
        checkNoOverflow(k < Integer.SIZE, "checkedPow", b, k);
        return ((k & 1) == 0) ? 1 << k : -1 << k;
      default:
        // continue below to handle the general case
    }
    int accum = 1;
    while (true) {
      switch (k) {
        case 0:
          return accum;
        case 1:
          return checkedMultiply(accum, b);
        default:
          if ((k & 1) != 0) {
            accum = checkedMultiply(accum, b);
          }
          k >>= 1;
          if (k > 0) {
            checkNoOverflow(-FLOOR_SQRT_MAX_INT <= b & b <= FLOOR_SQRT_MAX_INT, "checkedPow", b, k);
            b *= b;
          }
      }
    }
  }
public static int checkedPow(int b, int k) {
    checkNonNegative("exponent", k);
    switch (b) {
      case 0:
        return (k == 0) ? 1 : 0;
      case 1:
        return 1;
      case (-1):
        return ((k & 1) == 0) ? 1 : -1;
      case 2:
        checkNoOverflow(k < Integer.SIZE - 1, "checkedPow", b, k);
        return 1 << k;
      case (-2):
        checkNoOverflow(k < Integer.SIZE, "checkedPow", b, k);
        return ((k & 1) == 0) ? 1 << k : -1 << k;
      default:
        // continue below to handle the general case
    }
    int accum = 1;
    while (true) {
      switch (k) {
        case 0:
          return accum;
        case 1:
          return checkedMultiply(accum, b);
        default:
          if ((k & 1) != 0) {
            accum = checkedMultiply(accum, b);
          }
          k >>= 1;
          if (k > 0) {
            checkNoOverflow(-FLOOR_SQRT_MAX_INT <= b & b <= FLOOR_SQRT_MAX_INT, "checkedPow", b, k);
            b *= b;
          }
      }
    }
  }
public static int checkedPow ( int b , int k ) { checkNonNegative ( "exponent" , k ) ; switch ( b ) { case 0 : return ( k == 0 ) ? 1 : 0 ; case 1 : return 1 ; case ( - 1 ) : return ( ( k & 1 ) == 0 ) ? 1 : - 1 ; case 2 : checkNoOverflow ( k < Integer . SIZE - 1 , "checkedPow" , b , k ) ; return 1 << k ; case ( - 2 ) : checkNoOverflow ( k < Integer . SIZE , "checkedPow" , b , k ) ; return ( ( k & 1 ) == 0 ) ? 1 << k : - 1 << k ; default : // continue below to handle the general case } int accum = 1 ; while ( true ) { switch ( k ) { case 0 : return accum ; case 1 : return checkedMultiply ( accum , b ) ; default : if ( ( k & 1 ) != 0 ) { accum = checkedMultiply ( accum , b ) ; } k >>= 1 ; if ( k > 0 ) { checkNoOverflow ( - FLOOR_SQRT_MAX_INT <= b & b <= FLOOR_SQRT_MAX_INT , "checkedPow" , b , k ) ; b *= b ; } } } }
Returns the {@code b} to the {@code k}th power, provided it does not overflow.

<p>{@link #pow} may be faster, but does not check for overflow.

@throws ArithmeticException if {@code b} to the {@code k}th power overflows in signed {@code
int} arithmetic
Returns the { @code b } to the { @code k } th power provided it does not overflow .