public static double pow(double x, int n) {
    if (n == 0)
      return 1;
    if (x == 0)
      return 0;
    boolean pos = true;
    if (n < 0) {
      pos = false;
      n = -n;
    }
    double result = 1;
    double base = x;
    while (n > 0) {
      if ((n & 1) == 1) {
        result *= base;
      }
      base = base * base;
      n = n >> 1;
    }
    return pos ? result : 1 / result;

  }