1. In lecture we defined our own 8-bit floating-point number definition with the
ID: 3746154 • Letter: 1
Question
1. In lecture we defined our own 8-bit floating-point number definition with the following attributes: First bit represents the sign of the number .Bits 2-4 represent the signed exponent Bits 5-8 represent the mantissa Write a MATLAB function called my8BitNumber.m that takes a string representation of an 8-bit floating-point number as an input argument and returns the base-10 equivalent. a. e.g. >>inputStr'01101010 (Hint: use the st r2num function) A "skeleton code" outline of your function can be downloaded from Canvas. b. Test your code with the following floating-point numbers. Verify the results by hand: 01101010 11101010 01101100 00101100Explanation / Answer
(a) Please find the required MATLAB script. Read the comments very carefully.
%==========================================
function [dec_num] = my8BitNumber()
float_num = input('Please input a number: ','s');
% Separation of binary bits into three parts
sign = (-1)^(str2num(float_num(1)));
mantissa = bin2dec(strcat('1',float_num(5:8)));
% This is not the actual value of mantissa. This is the decimal representation
% of four bit left shifted mantissa from decimal point e.g. 1.0010 -> 10010
% These shfiting of 4 bits we take care in the final calculation by subtraction in exponent
exponent = bin2dec(float_num(2:4))-3;
% De-normalization and decimal value calculation
dec_num = sign * mantissa * 2^(exponent-4);
end
%==========================================
Output for the part (b)
01101010 --> 13
11101010 --> -13
01101100 --> 14
00101100 --> 0.8750
Note: The function file must be saved with the same name as the function. The calculated value is returned in the variable 'dec_num'
Hope this helps!
PLEASE THUMBS UP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.