src/org/kocakosm/pitaya/math/Numbers.java
public static long safePow(long a, int b)
	{
		Parameters.checkCondition(b >= 0);
		if (b == 0) {
			return 1L;
		}
		long base = a;
		int exponent = b;
		long result = 1L;
		try {
			while (exponent > 1) {
				if ((exponent & 1) != 0) {
					result = safeMultiply(result, base);
					exponent -= 1;
				}
				base = safeMultiply(base, base);
				exponent >>= 1;
			}
			return safeMultiply(result, base);
		} catch (ArithmeticException e) {
			throw new ArithmeticException(
				"Long overflow: " + a + " ^ " + b);
		}
	}
public static long safePow(long a, int b)
	{
		Parameters.checkCondition(b >= 0);
		if (b == 0) {
			return 1L;
		}
		long base = a;
		int exponent = b;
		long result = 1L;
		try {
			while (exponent > 1) {
				if ((exponent & 1) != 0) {
					result = safeMultiply(result, base);
					exponent -= 1;
				}
				base = safeMultiply(base, base);
				exponent >>= 1;
			}
			return safeMultiply(result, base);
		} catch (ArithmeticException e) {
			throw new ArithmeticException(
				"Long overflow: " + a + " ^ " + b);
		}
	}
Returns the value of the first argument raised to the power of the
second, provided the result fits into a {@code long}. This method
implements the exponentiation by squaring method. Please refer to
<a href="http://en.wikipedia.org/wiki/Exponentiation_by_squaring">
Wikipedia</a> for further information.

@param a the base.
@param b the exponent.

@return {@code a ^ b}.

@throws IllegalArgumentException if {@code b < 0}.
@throws ArithmeticException if {@code a ^ b} can't be represented by
a {@code long}.