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

1 visualización (últimos 30 días)
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

Respuestas (1)

Haseeb Hashim
Haseeb Hashim el 7 de Mzo. de 2022
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)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by