MATLAB: Get bits of number.

bit-wise operationsMATLAB

I have a 16 bit double I got from using wavread that I want to get the first 8 bits from.
As an example lets say I have 67. Inside my computer it must be represented as a series of bits, 0111001 in this case. So lets say that I just want to have the last for of them 1001.
How can this be done?

Best Answer

Assuming you have a 16 bit integer, the simplest way to extract some bits from it is to use bitget:
n = hex2dec('AF75'); %for example, bit pattern is 1010 1111 0111 0101.
bitpattern = bitget(n, 1:8, 'uint16')
returns
bitpattern =
1 0 1 0 1 1 1 0
You can also use dec2bin but that involves string conversion so is going to be slower.
Related Question