0% found this document useful (0 votes)
5 views3 pages

Chapter1 Prob36

The document describes a user-defined MATLAB function named 'deciTObina' that converts decimal numbers to a 30-element binary vector. The first 15 elements represent the integer part and the last 15 represent the fractional part, with a limitation on the size of the input number. It includes example executions of the function with specific decimal numbers and their corresponding binary outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Chapter1 Prob36

The document describes a user-defined MATLAB function named 'deciTObina' that converts decimal numbers to a 30-element binary vector. The first 15 elements represent the integer part and the last 15 represent the fractional part, with a limitation on the size of the input number. It includes example executions of the function with specific decimal numbers and their corresponding binary outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

1.36 Write a user-defined MATLAB function that converts real numbers in decimal form to binary form.
Name the function b = deciTObina(d), where the input argument d is the number to be converted and
the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary
form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of
b store the digits to the right of the decimal point. If more than 15 positions are required in the binary form
for the digits to the right of the decimal point, the digits should be chopped. If the number that is entered as
d is larger than can be stored in b, the function should display an error message. Use the deciTObina in
the Command Window to convert the numbers 85.321, 0.00671, and 3006.42.
Solution

function b=deciTObina(d)
large=0; b(1:30)=0;
if d > 0.0
sign = 1;
else
sign = 0;
end
for i=1:15
large=large+(2^(i-1));
end
if(d>large)
disp('ERROR: The number entered is too large for this function to handle')
end
dec = abs(d); integer = floor(dec); decimal = dec - integer; num = dec;
flag = 0; icount = 1;
%Convert digits to the right of the decimal point:
if(decimal~=0)
for j = 1:15
temp = decimal*(2^j);
if (temp >= 1)
p(icount) = -j; icount = icount + 1; decimal = decimal - (1/(2^j));
if (decimal == 0)

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2

flag = 1;
end
end
if (flag == 1)
break
end
end
for i=1:length(p)
b(15-p(i))=1;
end
end
%Convert digits to the left of the decimal point:
if(integer~=0)
flag = 0; icount = 1;
for j = 15:-1:1
temp = integer/(2^j);
if (temp >= 1)
pint(icount) = j; icount = icount + 1; integer = integer - (2^j);
if (integer == 1)
flag = 1; pint0 = 0;
end
end
if (flag == 1)
break
end
end
for i=1:length(pint)
b(15-pint(i))=1;
end
if (integer == 1)
pint((length(pint))+1) = pint0;

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3

end
for i=1:length(pint)
b(15-pint(i))=1;
end
end

When the function is executed in the command window, the following is obtained:
(a)
>> b=deciTObina(85.321)
b =
Columns 1 through 11
0 0 0 0 0 0 0 0 1 0 1
Columns 12 through 22
0 1 0 1 0 1 0 1 0 0 1
Columns 23 through 30
0 0 0 1 0 1 1 0

(b)
>> b=deciTObina(0.00671)
b =
Columns 1 through 11
0 0 0 0 0 0 0 0 0 0 0
Columns 12 through 22
0 0 0 0 0 0 0 0 0 0 0
Columns 23 through 30
1 1 0 1 1 0 1 1
>>
(c)
>> b=deciTObina(3006.42)
b =
Columns 1 through 11
0 0 0 1 0 1 1 1 0 1 1
Columns 12 through 22
1 1 1 0 0 1 1 0 1 0 1
Columns 23 through 30
1 1 0 0 0 0 1 0

Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.

You might also like