In MATLAB: 6. Write a function with header d my IEEE2Dec (IEEE) where IEEE is a
ID: 643863 • Letter: I
Question
In MATLAB:
6. Write a function with header d my IEEE2Dec (IEEE) where IEEE is a l x 32 amay of ones and zeros representing a 32-bit IEEE754 number. The output should be the equivalent decimal representation of IEEE. The input wariable IEEE will always be a 32-element array of ones and zeros defining a 32-bit single precision float Test Cases: IEEE [1 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 a my IEEE 2 Dec (IEEE 48 IEEE 1 1 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 1; d my IEEE Dec (IEEE) -2.128544921875000 et 003 IEEE zeros (1, 32) dl my IEEE Dec (IEEE) 5.8775e-039 IEEE ones (1, 32) dl my IEEE 2Dec (IEEE) -6.8056e+038Explanation / Answer
function [y]=IEEE2dec(x)
a=bin2dec(x(2:9),0); %% exponent
a=a-127;
a=2^a;
b=bin2dec(x(10:end),1);
y=a*(b+1);
if x(1)==1
y=-y;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% function definiton for bin2dec %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [y]=bin2dec(x,flag)
%% flag : it is to keep track of whether the value x is before decimal point or after
%% flag=0 for before decimal point
%% flag =1 for after decimal point
l=length(x);
y=0;
for i=1:l
if flag==0
y=y+2^(i-1)*x(l+1-i);
else
y=y+2^(-i)*x(i);
end
end
end
%%%%%%%%%%%%%%%%%%
%%% part 2 %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%
function [y]=dec2IEEE(x)
y=zeros(1,32);
if x<0
y(1)=1;
end
x=abs(x);
p=0;
while x>=2
x=x/2;
p=p+1;
end
x=x-1;
y(10:32)=dec2bin(x,23,1);
p=p+127;
y(2:9)=dec2bin(p,8,0);
end
%%%%%%%%%%%%%%%%%
%%%% function dec2bin %%%%
%%%%%%%%%%%%%%%%%%
function [y]=dec2bin(x,bits,flag)
%% flag is the variable keeping track of whether it is after decimal or before decimal
%% flag=1 : lesser than 1
%% flag0 : x is higher than 1 and an integer
%% bits will decide number of bits in the represtation
y=zeros(1,bits);
if flag==1
for i=1:bits
x=x*2;
if x>=1
y(i)=1;
x=x-1;
end
end
else
for i=1:bits
if rem(x,2)==0
y(bits+1-i)=0;
else
y(bits+1-i)=1;
end
x=floor(x/2);
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.