I am looking for something like
nextpow2 in MATLAB like this:
P = nextpow2(A)returns the exponents for the smallest powers of two that satisfy $2^P\geq\left|A\right|$.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.Sign up to join this community
You have a good answer already, but I'll mention the following since it may be useful in the future.
Many built-in functions are written in MATLAB and can be viewed, in this case
edit nextpow2.m brings up the source for this function, which can be used as a starting point to implement in Mathematica.
Not exactly the same, but very closely related is
BitLength[n]gives the number of binary bits necessary to represent the integer
BitLength[n]is effectively an efficient version of
n, it is equivalent to
When is it not equivalent to
It works for integers only.
When the argument is positive and an integer power of 2, the result of
BitLength will be one greater than that of