MATLAB: Efficient way to get MSB position of a double variable

bitgetbitshiftmsb

I am currently using the following equation to get the index of the most significant bit (MSB) of an integer value stored in a double variable.
xMsbIdx=ceil(log2(abs(x+1)))
I would like to know if there is a more computationally efficient way of doing it.
  • Edit for clarification propose

Best Answer

My guess is you're trying to get the index of the most significant bit of an integer, not a floating point double (where a number is divided in two parts, mantissa and exponent, so not sure what you'd call the msb).
To get that value:
xMsb = nextpow2(x);
is simpler but probably not as fast as your algorithm. You can look at the code for nextpow2, it is implemented as m file.