Efficient way to get MSB position of a double variable
Mostra commenti meno recenti
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
1 Commento
John D'Errico
il 10 Nov 2017
What is inefficient about this? This seems to be a very computationally efficient formula.
More importantly, why not explain what you mean by MSB position? If you choose some jargon that nobody else will understand, then you need to explain what you are doing.
Risposta accettata
Più risposte (1)
Walter Roberson
il 10 Nov 2017
0 voti
num2hex() of the value will give you the hex representation of the internal value. The first bit is the "separate sign" bit (two's complement is not used). The next 11 bits are the exponent, with a bias of 1023.
The File Exchange Contribution https://www.mathworks.com/matlabcentral/fileexchange/25326-ieee-754-binary-representation returns the breakdown as binary.
If performance is important, use a mex routine. Watch out for the fact that the values will be stored "little endian".
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!