Exam 1 Solutions
CS 213 Fall 2006

*********
Problem 1
*********

Fals (TMax)
True
False (all)
False (Tmin)
True
True
False (MSB==1, e.g., 0x80000000)
False (0, TMin)

*********
Problem 2
*********
Value	FP bits	Rounded value
7/8	010 11	7/8	no rounding needed
15/16	011 00	1	round up to even, renormalize
9	110 00	8	round down to even
10	110 01	10	no rounding needed

*********
Problem 3
*********

int foo (int n) {
    int a, i;

    a = 0;
    for (; n > 1; n--) {
        a = a + n;
        for (i = 0; i < n; i++)
            a = a + i;
    }
    return a;
}

*********
Problem 4
*********
H = 127 
J = 3 

*********
Problem 5
*********

Part A. 
   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 | vintages                          |xx xx xx xx|
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
  16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 | cost                  |z |xx xx xx xx xx xx xx|
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
  32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 | next                  | ages                   
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
  48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
       |xx xx| type      |a |xx xx xx xx xx xx xx|
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

Part B. How many bytes of space in WineNode are wasted? 20

Part C. 
 typedef struct WineNode {
     int vintages[3];
     short ages[5];
     char a;
     char z;
     int type;
     double cost;
     WineNode *next;
 } WineNode;
Note: there may be several correct solutions for Part C:

Part D: 
   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 | vintages                          | ages      
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
  16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
                   |a |z | type      |xx xx xx xx|
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
  32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 | cost                  | next                  |
 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
Part E.  How many bytes of space in this new WineNode are wasted? 4
Note: There is only one correct solution for Part E.

*********
Problem 6
*********
beta
delta
alpha
gamma

*********
Problem 7
*********
  
 long fun(long a, long b, long s)
  {
      long result = 0;
      switch (s) {
      case 3:
      case 7:
          result = b-1;
          break;
      case 6:
           b = b*a;
          /* Fall through */
      case 5:
          result = a & b;
          break;
      case 1:
          result = a+b;
          break;
      default:
          result = a*2;
      }
      return result;
  }