Week 03 Tutorial Questions

  1. On a machine with 16-bit ints, the C expression (30000 + 30000) yields a negative result.

    Why the negative result? How can you make it produce the correct result?

  2. Assume that the following hexadecimal values are 16-bit twos-complement. Convert each to the corresponding decimal value.

    1. 0x0013
    2. 0x0444
    3. 0x1234
    4. 0xffff
    5. 0x8000
  3. Give a representation for each of the following decimal values in 16-bit twos-complement bit-strings. Show the value in binary, octal and hexadecimal.

    1. 1
    2. 100
    3. 1000
    4. 10000
    5. 100000
    6. -5
    7. -100
  4. What decimal numbers do the following single-precision IEEE 754-encoded bit-strings represent?

    1. 0 00000000 00000000000000000000000
    2. 1 00000000 00000000000000000000000
    3. 0 01111111 10000000000000000000000
    4. 0 01111110 00000000000000000000000
    5. 0 01111110 11111111111111111111111
    6. 0 10000000 01100000000000000000000
    7. 0 10010100 10000000000000000000000
    8. 0 01101110 10100000101000001010000

    Each of the above is a single 32-bit bit-string, but partitioned to show the sign, exponent and fraction parts.

  5. Convert the following decimal numbers into IEEE 754-encoded bit-strings:

    1. 2.5
    2. 0.375
    3. 27.0
    4. 100.0
  6. Write a C function, six_middle_bits, which, given a uint32_t, extracts and returns the middle six bits.

  7. Draw diagrams to show the difference between the following two data structures:

    struct {
    	int a;
    	float b;
    } x1;
    union {
    	int a;
    	float b;
    } x2;
    

    If x1 was located at &x1 == 0x1000 and x2 was located at &x2 == 0x2000, what would be the values of &x1.a,   &x1.b,   &x2.a, and   &x2.b?

  8. How large (#bytes) is each of the following C unions?

    1. union { int a; int b; } u1;
      
    2. union { unsigned short a; char b; } u2;
      
    3. union { int a; char b[12]; } u3;
      
    4. union { int a; char b[14]; } u4;
      
    5. union { unsigned int a; int b; struct { int x; int y; } c; } u5;
      

    You may assume   sizeof(char) == 1,   sizeof(short) == 2,   sizeof(int) == 4.

  9. Consider the following C union

    union _all {
       int   ival;
       char cval;
       char  sval[4];
       float fval;
       unsigned int uval;
    };
    

    If we define a variable union _all var; and assign the following value var.uval = 0x00313233;, then what will each of the following printf(3)s produce:

    1.   printf("%x\n", var.uval);
    2.   printf("%d\n", var.ival);
    3.   printf("%c\n", var.cval);
    4.   printf("%s\n", var.sval);
    5.   printf("%f\n", var.fval);
    6.   printf("%e\n", var.fval);

    You can assume that bytes are arranged from right-to-left in increasing address order.

  10. Consider the following small C program:

    int a;              // global int variable
    
    int main (void)
    {
    	int b;      // local int variable
    	char c;     // local char variable
    	char d[10]; // local char array
    	...
    }
    
    int e;              // global? int variable
    
    int f (int g)       // function + parameter
    {
    	double h;   // local double variable
    	double *j;  // pointer to double object
    	...
    	j = malloc (sizeof (double));
    	...
    }
    

    Describe the following properties for each of the objects a, b, ... j, and *j

    • region (which part of the memory is the object located in)
    • size (how large is the object, in bytes)
    • scope (which part of the program can see the object)
    • lifetime (when is the object created, and when removed)
  11. Consider the following generic code which sets the value of a pointer (to a hopefully legal address on the stack), and subsequently increments it:

    Type *ptr = 0x7ffff000;
    ...
    ptr = ptr + 1;
    // what value does ptr have here?
    

    For each of the following variants of Type, show what the value of ptr would be after it is incremented:

    1. int
    2. short int
    3. char
    4. double
    5. struct xyz { int x; int y; int z; }
    6. struct abc { char a; int b; float c; }

    Make explicit assumptions about the sizes of various types.

  12. Consider the following C program fragment:

    int main (void)
    {
    	int x = 100;
    	char s[8];
    	int y = 200;
    	...
    	strcpy (s, "a long name");
    	...
    }
    

    If the memory looks like

    at the start of the program, show what it looks like after the strcpy(3) is executed.

  13. Without using any index variables in your function, implement a function myStrCmp(a,b) which behaves like the standard strcmp(a,b) function:

    • if a appears before b in the dictionary, return a negative value
    • if a appears after b in the dictionary, return a positive value
    • if a and b are the same string return 0

    You can assume that both strings are terminated by a '\0' character. Make sure you handle the case where the strings are different lengths, and one string is a sub-string of the other.

    Use the following function prototype:

    int myStrCmp (char *a, char *b);