I am converting decimal to binary. Then backward filling the binary in x2 array

1 回表示 (過去 30 日間)
hardik pandya
hardik pandya 2020 年 8 月 8 日
回答済み: Haseeb Hashim 2022 年 3 月 7 日
cinit=9;
rem=dec2bin(cinit);
rem=str2num(rem);
length=numel(num2str(rem))
for i=1:1:length
x2(i)=mod(rem,10);
rem=rem/10;
end
disp(x2(1:8));
Output for the code:
1.0000 0.1000 0.0100 1.0010 0 0 0 0
Expected Output :
1.0000 0.0000 0.0000 1.0000 0 0 0 0

回答 (1 件)

Haseeb Hashim
Haseeb Hashim 2022 年 3 月 7 日
Use this code
clc
clear
close all
num = 200;
i = 1;
flag = true;
while flag == true
bin(i) = rem(num,2);
if bin(i) == 0
num = num/2;
if num == 1
bin(i+1) = 1;
break
end
elseif bin(i) ~= 0
num = floor(num/2);
if num == 1
bin(i+1) = 1;
break
end
end
i = i + 1;
end
flip(bin)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by