50. My pow

#include <stdbool.h>

double myPow(double x, int n) {
  long long N = n; // -INT_MIN overflows int
  bool neg = N < 0;
  if (neg)
    N = -N;

  double res = 1;
  while (N > 0) {
    if (N & 1)
      res *= x;
    x *= x;
    N >>= 1;
  }
  return neg ? 1 / res : res;
}
class Solution {
public:
  double myPow(double x, int n) {
    long long N = n;
    bool neg = N < 0;
    if (neg)
      N = -N;

    double res = 1;
    while (N > 0) {
      if (N & 1)
        res *= x;
      x *= x;
      N >>= 1;
    }
    return neg ? 1 / res : res;
  }
};
package mathgeometry

func myPow(x float64, n int) float64 {
    N := n
    neg := N < 0
    if neg {
        N = -N
    }

    res := 1.0
    for N > 0 {
        if N&1 == 1 {
            res *= x
        }
        x *= x
        N >>= 1
    }
    if neg {
        return 1 / res
    }
    return res
}
class Solution {
    public double myPow(double x, int n) {
        long N = n; // -Integer.MIN_VALUE overflows int
        boolean neg = N < 0;
        if (neg)
            N = -N;

        double res = 1;
        while (N > 0) {
            if ((N & 1) == 1)
                res *= x;
            x *= x;
            N >>= 1;
        }
        return neg ? 1 / res : res;
    }
}
/**
 * @param {number} x
 * @param {number} n
 * @return {number}
 */
var myPow = function (x, n) {
  const neg = n < 0;
  let N = Math.abs(n); // bitwise ops would truncate 2^31 to int32

  let res = 1;
  while (N > 0) {
    if (N % 2 === 1) res *= x;
    x *= x;
    N = Math.floor(N / 2);
  }
  return neg ? 1 / res : res;
};
class Solution:
    def myPow(self, x: float, n: int) -> float:
        neg = n < 0
        N = -n if neg else n

        res = 1.0
        while N > 0:
            if N & 1:
                res *= x
            x *= x
            N >>= 1
        return 1 / res if neg else res
impl Solution {
    pub fn my_pow(mut x: f64, n: i32) -> f64 {
        let neg = n < 0;
        let mut big_n = (n as i64).abs(); // i32::MIN.abs() overflows

        let mut res = 1.0;
        while big_n > 0 {
            if big_n & 1 == 1 {
                res *= x;
            }
            x *= x;
            big_n >>= 1;
        }
        if neg { 1.0 / res } else { res }
    }
}