class Solution {
public:
double myPow(double x, int n) {
double ans = 1;
unsigned long long p;
if (n < 0) {
p = n;
x = 1 / x;
} else {
p = n;
}
while (p) {
if (p & 1)
ans *= x;
x *= x;
p >>= 1;
}
return ans;
}
};
Nonrecursive C++ log(n) solution

Thanks for your sharing.
There is a bug.
When n = INT_MIN, "n" is already overflow since 1 is int. The result of n is still INT_MIN.
Then p is therefore wrong since p is assigned from a negative but p is unsigned.
However the result with this bug can still pass because the only test case with INT_MIN is (1.0, INT_MIN). "n" should be change to "(1LL) * n" and p can be either unsigned or signed long long.
