I am learning MATLAB and am facing problem about extracting most significant bits from a given double. I saw getmsb function. But, is there a function that can give me say 5 most significant bits?
Anil.
I am learning MATLAB and am facing problem about extracting most significant bits from a given double. I saw getmsb function. But, is there a function that can give me say 5 most significant bits?
Anil.
It's late, so I'm sure there's a better solution. Anyway, this seems to do it:
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.
Or, as one liner:
A = rand(1, 1) * 10000
nBits = 5
wantedMSB = fix(A / 2^(ceil(log2(A)) - nBits))
[Edit] Btw, the getmsb
function is part of the Fixed-Point Toolbox, which might not be available on every MATLAB installation.