0% found this document useful (0 votes)
27 views

Lab 2 - Answers

The document discusses various data types in MATLAB including binary, unsigned integers, signed integers and strings. It provides exercises to convert decimals to binary, predict outputs of data type conversions, and introduces basic string operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lab 2 - Answers

The document discusses various data types in MATLAB including binary, unsigned integers, signed integers and strings. It provides exercises to convert decimals to binary, predict outputs of data type conversions, and introduces basic string operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab 2: Bits, Bytes and Data Types

Exercise 1 – Convert the following decimal numbers into binary: 6, 19, 47.

Answer: Done by hand.

Exercise 2 – Try to work out some systematic method of converting a decimal number to a
binary string without using a software program to do it for you.

Suggestion: work with a relatively large number like 181.

Answer: Done by hand.

Exercise 3 – Try the following commands in MATLAB and see if you can explain the output.

1. uint8(16.5) = 17 – the unsigned integer code rounds decimal number by adding one
when the digit after the decimal point is 5 or more.
2. uint8(16.2) = 16 – the unsigned integer code rounds the decimal number by just
removing the decimal part.
3. uint8(-47) = 0 – 8 bits only allow us to work with numbers from 0 to 255.
4. uint8(436) = 255 – 8 bits only allow us to work with numbers from 0 to 255.
5. uint16(436) = 436 – 16 bits allow us to work from numbers from -32768 to 32767.
6. uint16(1000000) = 65535 – 16 bits allow us to work from numbers from -32768 to
32767.
7. uint32(1000000) = 1000000 – 32 bits allow us to work from numbers from -
2147483648 to 2147483647.

Exercise 4 – Try to predict what the outcomes of the following commands in MATLAB will
be, then check your predictions using MATLAB.

1. int8(16.5) = prediction (17), MATLAB (17).


2. int8(-47) = prediction (-47), MATLAB (-47).
3. int8(-143) = prediction (-128), MATLAB (-128).
4. int8(436) = prediction (127), MATLAB (127).
5. int16(436) = prediction (436), MATLAB (436).
6. a = int8(60); 5*a = prediction (300), MATLAB (127).
7. 5*double(a) = prediction (300), MATLAB (300).

Exercise 5 – Intro to Strings. Do the following in MATLAB:

>> my_name = 'insert your name between single italics‘


>> my_name(1)
>> my_name(2)
>> my_name(100)
>> double(my_name)
>> 2*my_name
>> char([72 69 76 76 79])
MATLAB outcomes:

>> my_name = 'Edi'

my_name =

'Edi'

>> my_name(1)

ans =

'E'

>> my_name(2)

ans =

'd'

>> my_name(100)
Index exceeds the number of array elements (3).

>> double(my_name)

ans =

69 100 105

>> 2*my_name

ans =

138 200 210

>> char([72 69 76 76 79])

ans =

'HELLO'

You might also like