[go: up one dir, main page]

0% found this document useful (0 votes)
15 views2 pages

Chapter1 Prob34

The document describes a user-defined MATLAB function called binaTOint that converts binary integers to decimal form, with an input limit of 20 bits. It includes the function's code and examples demonstrating its use with specific binary numbers, yielding their decimal equivalents. An error message is displayed if the input exceeds the 20-bit limit.

Uploaded by

skibidipapap
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)
15 views2 pages

Chapter1 Prob34

The document describes a user-defined MATLAB function called binaTOint that converts binary integers to decimal form, with an input limit of 20 bits. It includes the function's code and examples demonstrating its use with specific binary numbers, yielding their decimal equivalents. An error message is displayed if the input exceeds the 20-bit limit.

Uploaded by

skibidipapap
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/ 2

1

1.34 Write a user-defined MATLAB function that converts integers written in binary form to decimal
form. Name the function d = binaTOint(b), where the input argument b is a vector with 1s and 0s that
represents the binary number to be converted and the output argument d is a number in decimal form. The
largest number that could be converted with the function should be a binary number with 20 1s. If a larger
number is entered for b, the function should display an error message. Use the function to convert the fol-
lowing numbers:
(a) 11010. (b) 10101100111. (c) 11100110001110101.
Solution

function d=binaTOint(b)
sum=0;
n=length(b);
if(n>20)
disp('ERROR: The number entered is too large for this function to handle');
end
for j=n:-1:1
sum=sum+(b(n-j+1)*(2^(j-1)));
d=sum;
end

When the function is executed in the command window, the following is obtained:
(a)
>> b=[1 1 0 1 0];
>> d=binaTOint(b)
d =
26

(b)
>> b=[1 0 1 0 1 1 0 0 1 1 1];
>> d=binaTOint(b)
d =
1383

(c)
>> b=[1 1 1 0 0 1 1 0 0 0 1 1 1 0 1 0 1];

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

>> d=binaTOint(b)
d =
117877

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