Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C string format, i.e., an array of char whose last entry is the character \0 ) and replaces all instances of a lowercase letter in the string with the corresponding uppercase letter. circle must not change the original string, rather it must return a pointer to a char array with the characters replaced. You may use only bitwise operations (, &, ^) to convert characters. You may not use arithmetic and you may not use a case or a nested if statement that checks for every lowercase letter. You may use if statements in other ways, for example to check whether a character is uppercase. The ASCII chart is attached at the end of this exam. You may declare two character pointers in your program, but my not use any other character variables, character pointer variables, or character or string constants. For example, you may not use the character constant A anywhere in your program, nor may you declare variables of type char. You may not use any string library functions in your solution. You may use loops (for and while both allowed). You may not use array syntax at all. Use C-style hexadecimal notation for all masks (i.e., 0xFF00) SOLUTION: char *circle(char* s){ int i = 0; char *temp, *temp2; // determine the size of s and create a new string temp = s; while (*temp++!= '\0') i++; temp = malloc(i * sizeof(char)); temp2 = temp;
while (*s!= '\0') { printf("s is: %c\n", *s); *temp = *s; if (*s > 96 && *s <= 111) { /* one way: *temp &= 0x0F; *temp = 0x40; */ // faster way: *temp &= 0x4F; if (*s > 111 && *s < 123) { /* one way: *temp &= 0x0F; *temp = 0x50; */ // faster way: *temp &= 0x5F; s++; temp++; return temp2;
2. (20 minutes) Answer the following questions about information representation. a. Do the following steps on each of the numbers given below. For all results assume that the binary numbers are in 2 s complement form. The number of bits available for the binary number is given beside the number. Show all your work at the bottom of this page. 1. Convert the following hexadecimal numbers to binary and then to decimal. Show the results. 2. Next do one ASL operations on the binary representation of each number showing the results in binary. Convert the shifted binary numbers to Hexadecimal and to decimal. Show the binary results and the hexadecimal and decimal results in the table. 3. Next do a ASR operation on the binary representation of the result from step 2. Then convert the shifted binary numbers to Hexadecimal and to decimal. Show the binary results and the hexadecimal results. 5D in 7 bits 3E in 7 bits binary: 101 1101 binary: 011 1110 decimal: 35 decimal: +62 ASL operation binary: 011 1010 binary: 111 1100 hexadecimal: 0x3A hexadecimal: 0x7C decimal: +58 decimal: -4 ASR operation binary: 001 1101 binary: 111 1110 hexadecimal: 0x1D hexadecimal: 0x7E decimal: +29 decimal: -2
b. Perform additions on the following two sets of numbers, assuming seven-bit 2 s complement binary representation. Show the effect on the carry and overflow bits. 001 0111 101 1101 + 011 1111 + 101 0111 C: 0 V: 1 101 0110 011 0100 C: 1 V: 1 3. (30 minutes) (20 points) For each of the following propositions of C code, indicate if they are true or false. If false, give a counterexample. We assume that the variables are declared as follows int x,y; unsigned ux; and initialized to some unknown value. You should formulate your counterexamples in terms of the word size w. I have given the first answer as an example. You may assume right shift is arithmetical. Problem Answer Reason if false x < 0 è ((x*2) < 0) False TMin ux >= 0 True x & 7 == 7 è (x<<30) < 0 True ux > -1 False Both treated as unsigned. ux=1 x > y è -x < -y False 1, TMin x * x >= 0 False TMin x > 0 && y > 0 è x + y > 0 False TMax, TMax (overflow) x >= 0 è -x <= 0 True All pos num can be represented as neg num x <= 0 è -x >= 0 False TMin
4. Provide answers to the following pointer & dynamic memory questions a. Consider the declaration below. Change this declaration into a pointer declaration and give the code to dynamically create the 2D array. Both dimensions must be declared dynamically. Do not use a loop. int ARR1[ROWS][COLS]. int *ARR1 = (int **) malloc (sizeof(int) * ROWS * COLS) 5. Solve the below problems following these instructions. For more examples, see the lecture slides. Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations! ~ 4. Binary integer operations & ^ + << >> Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line. You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&,, -, or?: 6. Use any form of casting. 7. Use any data type other than int. This implies that you cannot use arrays, structs, or unions. /* evenbits - return word with all even-numbered bits set to 1 * Legal ops:! ~ & ^ + << >> * Max ops: 8 * Rating: 1 */ int evenbits(void) {
int byte = 0x55; int word = byte byte<<8; return word word<<16;