フィルターのクリア

Efficient way to get MSB position of a double variable

9 ビュー (過去 30 日間)
Jose Marcelo Duarte
Jose Marcelo Duarte 2017 年 11 月 10 日
コメント済み: Jose Marcelo Duarte 2017 年 11 月 16 日
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 件のコメント
John D'Errico
John D'Errico 2017 年 11 月 10 日
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.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 11 月 10 日
編集済み: Guillaume 2017 年 11 月 10 日
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.
  9 件のコメント
Guillaume
Guillaume 2017 年 11 月 13 日
@Stephen,
Noted. My misuse is probably because in French, as far as I know, the only term in use is mantisse.
Even in english, wikipedia says that This digit string is referred to as the significand, mantissa, or coefficient but I'll use significand from now on.
Jose Marcelo Duarte
Jose Marcelo Duarte 2017 年 11 月 16 日
I just found out about MatLab [F, E] = log2(x) function! This function is the perfect tool for design/model the word-width limitation of my RTL model.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 11 月 10 日
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".

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by