Assignment #2: Integer Operations, Floating-Point, and C



Topic: Operating on binary integer and floating point numbers. Arithmetic (+, -, *, /, %), Boolean (&, |, ^, ~), and shifts (<<, >>)
Related Reading: Section 2.3 and class notes

Practice Problems

Practice problems from the textbook (answers are at the end of the chapter):

Problems to be Submitted (25 points)

When you turn in your assignment, you must include a signed cover sheet (PDF version) with your assignment (you're assignment will not be graded without a completed cover sheet).

You are allowed to submit your assignment via email, but if you choose to do so, you must bring a hardcopy of your assignment along with a completed cover sheet to the instructor at the next class. (Note: Do not email the instructor any .zip file attachments, as SLU's email may not accept these emails; i.e. the instructor may not receive your email.)

Answers without work will recieve zero credit.

  1. (5 points)    Operations on Data:
      Compute results for the following data operations.

    Give your answer in binary or hex, and be sure to show your work

    1.   0x6E42B   -   0x37CA
    2.   10011010001012   >>   5
    3.   0x07AD4   ^   0x03B5D
    4.   (23   <<   4) - 0xFF
    5.   0x9C % 6
  2. (6 points)    Floating-Point Representations:

    1. Given the following IEEE 32-bit single-precision floating-point representation, give its equivalent decimal value:
             01000111 00110100 11010000 00000000
    2. Convert the base-2 number 10101.1100110011 into IEEE single-precision floating point.
    3. Convert the decimal number -936.4375 into IEEE double-precision floating-point.

  3. (4 points)    Shifts and Bit Manipulation for Fast Multiplies and Divides:

      Note: While these four questions provide an example value for x (so that you may solve for y if desired), what I'm really looking for is the value of K assuming any (valid) value of x.

    1. The following code corresponds to the mathematical equation: y = K * x, for some fixed value of K. In this case, what is K?
    2.       x = 11
            y = (x << 4) - x
    3. The following code similarly corresponds to: y = K * x, but for what value of K?
    4.       x = 7
            y = (x << 5) - (x << 2) + x;
    5. The following code corresponds to the mathematical equation: y = K * x, for some fixed value of K < 1. What is the value of K?
    6.       x = 35
            y = (x >> 1) - (x >> 3)
    7. Relative to multiplication and division, what mathematical equation does the following code produce?
    8.       x = 102
            y = x & 0x0f
  4. (8 points)    Pseudocode using Integer Operations:
          a = 8
          b = 29
          c = 0
          while (b >= a)
             b = b - a;
             c = c + 1;
          print c
          print b
    1. For the given values of a and b, what is printed?
    2. If the initial values of a and b were instead a = 4 and b = 17, what would be printed?
    3. How about if the initial values of a and b were a = 8 and b = 4 ?
    4. How about if the initial values of a and b were a = 5 and b = 25 ?
    5. What mathematical operation does this pseudocode perform? For arbitrary values of a and b, what does the final result in b correspond to? How about the final c value?
    6. Create a C program that asks the user to enter values for a and b, then computes the values for b and c, as shown in pseudocode above, and finally print out these two results.
            Please email your .c file to instructor (as well as turning in a hardcopy).
  5. (2 points)

    In image processing, a smoothing mask is sometimes used to soften hard edges and smooth out random noise. This is done by taking a weighted average of the pixel itself with its nearest neighbors.

    For example, given the following common 3x3 smoothing filter like the following, when centered over a pixel at p[y][x]

    the equation/code for smoothing that pixel is expressed as:

          p[y][x] = (4 * p[y][x] + p[y][x-1] + p[y][x+1] + p[y-1][x] + p[y+1][x]) / 8; 
    Since multiplies and divides are time-consuming operations, give a version of this equation/code that uses much more efficient (i.e faster) operations.