Tutorial solutions 1
1. How would the denary (base-10) number -25 be expressed in 8-bit 2s complement binary?
11100111
2. The population of Selly Oak (during term time) is 34,173. What is the smallest integer data type in the standard C programming language that could represent this number?
unsigned short int or uint16_t
You need to be careful here about whether numbers are signed or unsigned. The short int type is guaranteed to be at least 16 bits in any compiler that obeys the standard1, but 16-bit signed can only represent numbers in the range -32,768 up to 32,767. Unsigned 16 bit can represent numbers in the range 0 to 65,535. Population is a number that cannot be negative.
3. In the C language, a variable x is declared as follows: uint8_t x=0xB1;
What value does x have after the following line of code runs x |= ( (1<<3) | (1<<2) );
0xB1 is 10110001
( (1<<3) | (1<<2) ) is 00001100 So x gets the result 10111101
4. In the C language, a variable x is declared as follows: uint8_t x=0xB1;
What value does x have after the following line of code runs x &= (1<<4);
0xB1 is 10110001
(1<<4) is 00010000
So x gets the result 00010000
5. In the C language, a variable x is declared as follows: uint8_t x=0xB1;
What value does x have after the following line of code runs x &= ~(1<<4) ;
OxB1 is 10110001
(1<<4) is 00010000
~(1<<4) is 11101111
So x gets the result 10100001
1 Beware: some embedded compilers don¡¯t obey the standard.
6. In the C language, a variable x is declared as follows: uint8_t x=0xB1;
What value does x have after the following line of code runs x ^= (1<<4) ;
OxB1 is 10110001
(1<<4) is 00010000
So x gets the result 10100001
7. A velocity sensor measures velocities in the range 0 to 10 ms-1 and outputs a voltage in the range 0 to 2V.
a) An 8-bit ADC is used to convert the velocity sensor output voltage, using VREF set to 5V. Calculate the resolution of the measurement (expressed both in V and in ms-1) and also the maximum error.
Voltage resolution = 5/256 = 19.5 mV
Maximum voltage error = 5/512 = 9.77 mV
The maximum value of velocity only corresponds to 2/5 of the full voltage range The velocity resolution would therefore be 10/256 x 5/2 = 0.0977 ms-1
The maximum velocity error would be 0.0488 ms-1
b) The same 8-bit ADC is used, now with VREF set to 2.5V. Calculate the resolution of the measurement and also the maximum error.
Voltage resolution = 2.5/256 = 9.77 mV
Maximum voltage error = 2.5/512 = 4.88 mV
The maximum value of velocity only corresponds to 2/2.5 of the full voltage range The velocity resolution would therefore be 10/256 x 2.5/2= 0.0488 ms-1
The maximum velocity error would be 0.0244 ms-1