public class PowerCalculation {
// Method to compute integer exponentiation
public static double myPow(double base, int exponent) {
double result = 1;
while (exponent > 0) {
if ((exponent & 1) == 1) {
result *= base;
}
exponent >>= 1;
base *= base;
}
return result;
}
public static void main(String[] args) {
// Testing the function
System.out.println(myPow(2, 3)); // 8
System.out.println(myPow(3, 3)); // 27
System.out.println(myPow(3, 2)); // 9
System.out.println(myPow(2, 2)); // 4
System.out.println(myPow(5, 2)); // 25
System.out.println(myPow(-2, 4)); // 16
}
}
/*
run:
8.0
27.0
9.0
4.0
25.0
16.0
*/