extracting bit packed data

5 ビュー (過去 30 日間)
Muhammad
Muhammad 2012 年 2 月 15 日
コメント済み: Walter Roberson 2023 年 1 月 22 日
Hi
I have an matrix with dimensions 5 x 200 x 100, I want to extract the data encoded in the 2nd and 3rd bit of the first byte (first byte means 1 x 200 x 100).
similarly i would need the data encoded in the 4th and 5th bit also.
Please note that by "data" i mean a digit between 0-3 (as it is being extracted from 2 bits)
Best regards, Muhammad
  1 件のコメント
Muhammad
Muhammad 2012 年 2 月 15 日
移動済み: Voss 2023 年 1 月 21 日
waiting for the answer.

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

回答 (3 件)

Walter Roberson
Walter Roberson 2012 年 2 月 15 日
There are arithmetic means, and there is bitget()
  2 件のコメント
Muhammad
Muhammad 2012 年 2 月 16 日
Thanks walter. But I have used bitget and got following response.
??? Undefined function or method 'bitget' for input arguments of type 'int8'.
additionally, bit get can give me value of one bit (i.e. either 0 or 1). Where as i need the integer value of combined bits 2-3 (so the answer should be an integer among 0,1,2 or 3.
Walter Roberson
Walter Roberson 2023 年 1 月 22 日
bitget(int8(23), 1:5)
ans = 1×5
1 1 1 0 1
But that is current day; perhaps int8 was not supported back in 2012.

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


Jan
Jan 2012 年 2 月 15 日
x = floor(rand(5, 200, 100) * 256);
a = rem(x(1, :, :) / 2, 4);
Are these the 2nd and 3rd "bits" you are looking for? It depends on the counting direction and if you start with 0 or 1.
See also bitget.
  1 件のコメント
Muhammad
Muhammad 2012 年 2 月 16 日
Thanks Jan. No I am not looking for the bits themselves. I am looking for the integers packed in 2-3 bits. So the answer I am looking for should be an integer among 1,2,3 or 4

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


Floris Jansen
Floris Jansen 2023 年 1 月 21 日
Bit late with an answer, but this should work for you:
Note that if the bits are not contiguous you could still extract the data, but it will be a bit more work...
% start with a multi dimensional array of uint8
% (any number of dimensions should work)
myData = uint8(randi(255,[10,20,30]));
% create a mask with ones in the bit positions you want:
mask1 = 0b00110000; % extract 2 bits in positions 2,3
mask2 = 0b00011100; % extract 3 bits in positions 3,4,5
% now do bit and, and shift to get the number
data1 = bitand(myData, mask1)/16; % shift right by 4
data2 = bitand(myData, mask2)/4; % shift right by 2
% observe the results:
figure
subplot(2,1,1)
histogram(data1(:));
subplot(2,1,2);
histogram(data2(:));

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by