我正在学习 MATLAB,并且面临从给定双精度中提取最高有效位的问题。我看到了 getmsb 函数。但是,有没有一个函数可以给我说 5 个最高有效位?
阿尼尔。
已经很晚了,所以我相信有更好的解决方案。无论如何,这似乎做到了:
A = rand(1, 1) * 10000
nBits = 5
curBits = ceil(log2(A))
toShift = curBits - nBits
wantedMSB = fix(A / 2^toShift) % This is still a double, feel free to cast.
dec2bin(wantedMSB) % Result in bitstring form.
或者,作为一个班轮:
A = rand(1, 1) * 10000
nBits = 5
wantedMSB = fix(A / 2^(ceil(log2(A)) - nBits))
[编辑]顺便说一句,该getmsb
函数是定点工具箱的一部分,它可能并非在每个 MATLAB 安装中都可用。