Monash University > School of Computer Science and Software Engineering > CSE1303 > Part B > Tutorials > Tutorial B2

CSE1303 Computer Science
Semester 2, 2003
Part B
Tutorial B2

This tute covers material from lectures B04 to B06.

Attempt the questions marked (*) like this before the tutorial. If you have specific questions about unmarked questions, you can ask the tutor about them during the tutorial.

  1. Pointers and pointer arithmetic

    1. What does the following function compute?
      unsigned int mystery(const char *s)
      {
          const char *t;
      
          for(t=s; *t != 0; t++)
          {
              /* spin wheels */
          }
      
          return t - s;
      }
      
    2. (*) Consider the code for implementing a stack given in Part A of the lecture notes. The top of stack is indicated by an integer index.

      Rewrite the functions push and pop with the top of stack being indicated by a pointer instead. That is, the definition for the structure is now

      struct StackRec
      {
          float entry[MAXSTACK];
          float *topPtr;
      };
      typedef struct StackRec Stack;
      

      Do not worry about testing for the stack being empty or full. You will need to use pointer arithmetic to adjust the top-of-stack pointer upon push and pop.

  2. Byte order

    1. (*) Devise a method for determining the byte order (little or big endian) of a computer.

      (Hint: Consider the following code:

      unsigned long x; unsigned long *xptr;
      unsigned char c; unsigned char *cptr;
      
      x = 0x12345678;
      xptr = &x;                      /* get address of x */
      cptr = (unsigned char *) xptr;  /* pretend same address points to char instead */
      c = *cptr; 
      
      What value can you expect to see in c? How does this value differ on big-endian and little-endian computers?)
  3. Floating point numbers

    For reference, here are the sizes in bits of the components of the C floating-point types float, double and long double:
    formatsignexponentmantissa (including implicit bit)excess
    float1824127
    double111531023
    long double1156416383

    1. Which component of a floating point number affects its
      1. range?
      2. precision?
    2. (*) In the lectures, the range and precision of the C type float were computed. For C double and long double, calculate the following:
      1. smallest possible binary exponent
      2. largest possible binary exponent
      3. smallest representable positive number (not counting denormalized values)
      4. largest representable positive number
    3. What C float values do the following patterns of bits represent?
      1. (*) 0 00000000 00000000000000000000000
      2. 0 01111111 00000000000000000000000
      3. (*) 1 01111111 00000000000000000000000
      4. (*) 0 01111111 10000000000000000000000
      5. 0 10000000 10000000000000000000000
      6. 0 10000000 01010101010101010101010
      7. 0 11111111 00000000000000000000000
    4. How would you represent the following floating-point values as C floats?
      1. -1.0
      2. 32.0
      3. 42.0
      4. 0.75
      5. 0.2
    5. What advantages are there to representing floating-point numbers in normalized form (i.e., 1.000... <= mantissa <= 1.111...)?
  4. Bitwise operations

    1. (*) Complete the following truth table for addition of two bits A and B. In this table, S represents the sum, and C the carry.
      ABSC
      00  
      01  
      10  
      11  

      Comment on S and C from a logical-operation perspective (i.e., what logical functions can be used to implement them?).

    2. (*) Write a C function int bit(unsigned long x,unsigned int n) which returns the value of the nth bit of the binary integer x. n should be 0 for the least significant bit and 31 for the most significant bit.
    3. (*) Write C code to extract the following components from a C float:
      • sign
      • mantissa
      • exponent

      (To retain the bit pattern in the float, you will need to convert it to an unsigned long using a similar trick to the one in the "Byte Order" section above. If you don't, C will round the float to the nearest integer, which is not what you want.)

[ Top | Home ]

Last modified 2002-07-03