Returns the next largest power of two, or zero if x is already a power of two.

TODO(jgw): Is there no better way to do this than all this bit twiddling?
552604
public static int nextPowerOfTwo(int x) {
    assert x < 0x10000;

    int bit = 0x8000, highest = -1, count = 0;
    for (int i = 15; i >= 0; --i, bit >>= 1) {
      if ((x & bit) != 0) {
        ++count;
        if (highest == -1) {
          highest = i;
        }
      }
    }
    if (count <= 1) {
      return 0;
    }
    return 1 << (highest + 1);
  }
1179597
def next_power_of_2(n):
    
    n -= 1  # greater than OR EQUAL TO n
    shift = 1
    while (n + 1) & n:  # n+1 is not a power of 2 yet
        n |= n >> shift
        shift *= 2
    return max(4, n + 1)
21368
function nbits(x) {
	var n = 1, t;
	if ((t = x >>> 16) != 0) {
		x = t;
		n += 16;
	}
	if ((t = x >> 8) != 0) {
		x = t;
		n += 8;
	}
	if ((t = x >> 4) != 0) {
		x = t;
		n += 4;
	}
	if ((t = x >> 2) != 0) {
		x = t;
		n += 2;
	}
	if ((t = x >> 1) != 0) {
		x = t;
		n += 1;
	}
	return n;
}
276203
public static int gcd(int a, int b) {
    /*
     * The reason we require both arguments to be >= 0 is because otherwise, what do you return on
     * gcd(0, Integer.MIN_VALUE)? BigInteger.gcd would return positive 2^31, but positive 2^31
     * isn't an int.
     */
    checkNonNegative("a", a);
    checkNonNegative("b", b);
    if (a == 0) {
      // 0 % b == 0, so b divides a, but the converse doesn't hold.
      // BigInteger.gcd is consistent with this decision.
      return b;
    } else if (b == 0) {
      return a; // similar logic
    }
    /*
     * Uses the binary GCD algorithm; see http://en.wikipedia.org/wiki/Binary_GCD_algorithm.
     * This is >40% faster than the Euclidean algorithm in benchmarks.
     */
    int aTwos = Integer.numberOfTrailingZeros(a);
    a >>= aTwos; // divide out all 2s
    int bTwos = Integer.numberOfTrailingZeros(b);
    b >>= bTwos; // divide out all 2s
    while (a != b) { // both a, b are odd
      // The key to the binary GCD algorithm is as follows:
      // Both a and b are odd.  Assume a > b; then gcd(a - b, b) = gcd(a, b).
      // But in gcd(a - b, b), a - b is even and b is odd, so we can divide out powers of two.

      // We bend over backwards to avoid branching, adapting a technique from
      // http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax

      int delta = a - b; // can't overflow, since a and b are nonnegative

      int minDeltaOrZero = delta & (delta >> (Integer.SIZE - 1));
      // equivalent to Math.min(delta, 0)

      a = delta - minDeltaOrZero - minDeltaOrZero; // sets a to Math.abs(a - b)
      // a is now nonnegative and even

      b += minDeltaOrZero; // sets b to min(old a, b)
      a >>= Integer.numberOfTrailingZeros(a); // divide out all 2s, since 2 doesn't divide b
    }
    return a << min(aTwos, bTwos);
  }
1106866
static float powerOfTwoF(int n) {
        assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
        return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
                                     (FloatConsts.SIGNIFICAND_WIDTH-1))
                                    & FloatConsts.EXP_BIT_MASK);
    }
657169
static double pow(final double a, final double b) {
        long tmp = Double.doubleToLongBits(a);
        long tmp2 = (long) (b * (tmp - 4606921280493453312L)) + 4606921280493453312L;
        return Double.longBitsToDouble(tmp2);
    }
973635
def LargestComponent(self):
    
    if abs(self.x) > abs(self.y):
      if abs(self.x) > abs(self.z):
        return (0, self.x)
      else:
        return (2, self.z)
    else:
      if abs(self.y) > abs(self.z):
        return (1, self.y)
      else:
        return (2, self.z)
528118
public Rational pow(int exponent) {
        if (exponent == 0) {
            return new Rational(1, 1);
        }
        BigInteger num = a.pow(Math.abs(exponent));
        BigInteger deno = b.pow(Math.abs(exponent));
        if (exponent > 0) {
            return (new Rational(num, deno));
        } else {
            return (new Rational(deno, num));
        }
    }
738417
public static int ipowi(int x, int p) {
    if(p <= 2) {
      return (int) FastMath.powFast(x, p);
    }
    int tmp = x, ret = (p & 1) == 1 ? x : 1;
    while(true) {
      tmp *= tmp;
      p >>= 1;
      if(p == 1) {
        return ret * tmp;
      }
      if((p & 1) != 0) {
        ret *= tmp;
      }
    }
  }
230337
def power(n)
      raise ArgumentError, 'Cannot raise a temperature to a power' if temperature?
      raise ArgumentError, 'Exponent must an Integer' unless n.is_a?(Integer)
      return inverse if n == -1
      return 1 if n.zero?
      return self if n == 1
      return (1..(n - 1).to_i).inject(self) { |acc, _elem| acc * self } if n >= 0
      (1..-(n - 1).to_i).inject(self) { |acc, _elem| acc / self }
    end
1223638
def _call(self, x):
        """"""
        if self.exponent == 0:
            return self.domain.one().inner(np.not_equal(x, 0))
        elif self.exponent == 1:
            return x.ufuncs.absolute().inner(self.domain.one())
        elif self.exponent == 2:
            return np.sqrt(x.inner(x))
        elif np.isfinite(self.exponent):
            tmp = x.ufuncs.absolute()
            tmp.ufuncs.power(self.exponent, out=tmp)
            return np.power(tmp.inner(self.domain.one()), 1 / self.exponent)
        elif self.exponent == np.inf:
            return x.ufuncs.absolute().ufuncs.max()
        elif self.exponent == -np.inf:
            return x.ufuncs.absolute().ufuncs.min()
        else:
            raise RuntimeError('unknown exponent')
142446
def next_up(x, context=None):
    
    x = BigFloat._implicit_convert(x)
    # make sure we don't alter any flags
    with _saved_flags():
        with (context if context is not None else EmptyContext):
            with RoundTowardPositive:
                # nan maps to itself
                if is_nan(x):
                    return +x

                # round to current context; if value changes, we're done
                y = +x
                if y != x:
                    return y

                # otherwise apply mpfr_nextabove
                bf = y.copy()
                mpfr.mpfr_nextabove(bf)
                # apply + one more time to deal with subnormals
                return +bf
1014368
function nextPow2(size) {
  return Math.pow(2, Math.ceil(Math.log(size) / Math.LN2));
}
758350
public static float max(float[] x) {
        float m = Float.NEGATIVE_INFINITY;

        for (float n : x) {
            if (n > m) {
                m = n;
            }
        }

        return m;
    }
759058
@Pure
	@Inline(value="$3.pow($1, $2)", imported=Math.class)
	public static double operator_power(char a, int b) {
		return Math.pow(a, b);
	}
646210
@Inline(value="$1.pow($2)")
	@Pure
	public static BigInteger operator_power(BigInteger a, int exponent) {
		return a.pow(exponent);
	}
1061171
private int modularExponentation(int a, int b, int n) throws IllegalArgumentException {
        int result = 1;
        int counter;
        int maxBinarySize = 32;
        boolean[] b2Binary = new boolean[maxBinarySize];
        for (counter = 0; b > 0; counter++) {
            if (counter >= maxBinarySize){
                throw new IllegalArgumentException("Exponent "+b+" is too big !");
            }
            b2Binary[counter] = (b % 2 != 0);
            b = b / 2;
        }
        for (int k = counter - 1; k >= 0; k--) {
            result = (result * result) % n;
            if (b2Binary[k]){
                result = (result * a) % n;
            }
        }
        return result;
    }