lecture3_new - LAB 3 C Programming Techniques...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: LAB 3 C Programming Techniques ……Pointers, Bitwise Operations and More Rajesh Rajamani ME 4231 Department of Mechanical Engineering University Of Minnesota POINTERS p points at k the address of k is stored in p k is called the referent The expression *p stands for the referent If k is the referent of the pointer p, then m=*p; means the same thing as m = k; Similarly *p = n; means the same thing as k =n; 1 POINTERS Example int int int int *p; k =17; m; *pt = NULL; // An int pointer variable, initialized to NULL p = &k; m = *p + 2; *p = m; pt = p; POINTERS AND ARRAYS Arrays The name of an array is the same as the address of its zero’th element array and &array[0] both contain the address of the zero’th element An array can be passed as an argument to a function simply by passing the name of the array Passing an array is like passing a pointer Changes in the array made in the function are reflected back in the main program 2 TASKS IN LAB Task 1 Modify the sorting program from Lab 1 to make it modular Write a function called sort that accepts an integer array, sorts it, and returns the sorted array to the main function. Call this function from the main program. In addition, write a function called print_array that accepts an integer array and prints it out in this format: The array is [0 1 2 3 4 5 6 7 8 9] Use this function to print out the array before and after sorting. ORDER ARRAY ELEMENTS Determine i th element, i =0,1,2, ….,n i =0 i =1 Find smallest number among elements i , i +1, i +2,….,n Place the smallest number in the i th element This can be done using 2 “for loops” i =0,1,2,3,…..,n j =i+1,i+2,i+3,…..,n i j =i+1,i+2,….,n Compare i th element with j th element and swap if j th element is smaller 3 BIT OPERATIONS Bitwise operation Symbol AND OR EXCLUSIVE-OR NOT Shift right Shift left & | ^ ~ >> << BIT OPERATIONS “&” AND Operator A = 0xa2; B = 0x34; C = A&B; A= 1010 0010 = 0xa2 B= 0011 0100 = 0x34 A&B = 0010 0000 = 0x20 “|” OR operator A= B= A|B = 1010 0010 0011 0100 1011 0110 = 0xa2 = 0x34 = 0xb6 “^” XOR operator A= B= A^B = 1010 0010 0011 0100 1001 0110 = 0xa2 = 0x34 = 0x96 4 BIT OPERATIONS “<<” Left shift operator Assume A is a short int, i.e. a 16bit integer. A = 0xa234 = 1010 0010 0011 0100 A<<4 = 0x2340 = 0010 0011 0100 0000 A<<2 = 0x88d0 = 1000 1000 1101 0000 Vacancies in the right most bits are filled with zeroes. BIT OPERATIONS “>>” Right shift operator: A = 0xa234 = A>>4 = 0x0a23 = 1010 0010 0011 0100 0000 1010 0010 0011 Vacancies in the left most bits are filled with zeroes. 5 TASKS IN LAB Task 2 Write a function called bit_merge that accepts 2 four digit hexadecimal numbers (16 bits each) and combines their bits into a single 32 bit number as follows: void main() { unsigned short hn1 = 0x1234; unsigned short hn2 = 0xabcd; int result; result = bit_merge(hn1,hn2); printf(“\n Merging 0x%x and 0x%x results in \n”, hn1, hn2, result); } TASKS IN LAB Task 2 The output should look like: merging 0x1234 and 0xabcd results in 0xabcd1234 To do this requires a left shift of hn2 by 16 bits and then a bitwise OR of hn1 with the left-shifted hn2. Remember that hn2 needs to be left shifted into a 32 bit variable, or all the data will be lost when shifted. Be careful of data types (int versus unsigned short), and think about what is going on at a bit level. 6 TASKS IN LAB Task 3 Write a function that accepts a decimal number between 0 and 255 and prints it out on the screen using 8 binary digits for representation. void binary_print(short a) { // Print an error message if a is not between 0 and 255 // Find LSB and print on screen // Find Next MSB and print on screen // …. // Find MSB and print on screen } TASKS IN LAB Task 4 Parity method for error detection Assume that your data sequence has 7 bits and an eighth bit is added to achieve even parity. Write a program that accepts an 8-bit word (or equivalently, a 2 digit hex number) and does an even parity check on the input data. If the input data violates even parity, print out an error message 7 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online