フィルターのクリア

Convert vector of numeric values into a vector of equivalent 8-bit 'int8' binary values

5 ビュー (過去 30 日間)
Hi,
I am trying to convert a 1x4 numeric vector into the equivalent 8-bit binary 'int8' value which is also a 1x4 vector, however I am creating a 1x8 vector which appears to be the binary value of 8 which is the second element.
So each numeric element I want to convert to it's 8-bit 'int8' binary equivalent and store it in the same element in another vector.
Assuming I have not messed up my Two's Compliment I am expecting the row vector to be displayed as;
[10011110 01111000 01001111 00001000]
Any help would be appreciated.
x = [-98 8 49 120];
y = zeros(1,length(x));
for idx = 1:length(x)
y = bitget(x(idx),8:-1:1,'int8');
end
disp(y)
0 1 1 1 1 0 0 0

採用された回答

dpb
dpb 2024 年 6 月 19 日
x = [-98 8 49 120];
cellstr(dec2bin(int8(x)))
ans = 4x1 cell array
{'10011110'} {'00001000'} {'00110001'} {'01111000'}
  2 件のコメント
Dan Lardner
Dan Lardner 2024 年 6 月 20 日
Thanks for that.
I realised also that I applied the Two's Compliment to my positive integers which was a mistake.
I have not used cell arrays before, but I tried to include it in my original solution. It's not as efficient as your implementation.
x = [-98 8 49 120];
y = cell(length(x),1);
for idx = 1:length(x)
y{idx} = bitget(x(idx),8:-1:1,'int8');
end
disp(y)
{[1 0 0 1 1 1 1 0]} {[0 0 0 0 1 0 0 0]} {[0 0 1 1 0 0 0 1]} {[0 1 1 1 1 0 0 0]}
Walter Roberson
Walter Roberson 2024 年 6 月 20 日
cellstr(dec2bin(int8(x), 8))
would make more sense.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by