Assignment #2: Integer Operations, Floating-Point, and C
Contents:
Overview
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):
-
Practice Problem 2.1, on p. 37.
-
Practice Problem 2.4, on p. 39.
-
Practice Problem 2.8, on p. 51.
-
Practice Problem 2.14, on p. 57.
-
Practice Problem 2.16, on p. 58.
-
Practice Problem 2.40, on p. 103.
-
Practice Problem 2.44, on p. 108.
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.
- (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
- 0x6E42B - 0x37CA
- 10011010001012 >> 5
- 0x07AD4 ^ 0x03B5D
- (23 << 4) - 0xFF
- 0x9C % 6
- (6 points) Floating-Point Representations:
- Given the following IEEE 32-bit single-precision floating-point representation, give its equivalent decimal value:
01000111 00110100 11010000 00000000
- Convert the base-2 number 10101.1100110011 into IEEE single-precision floating point.
- Convert the decimal number -936.4375 into IEEE double-precision floating-point.
- (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.
- The following code corresponds to the mathematical equation: y = K * x, for some fixed value of K. In this case, what is K?
x = 11
y = (x << 4) - x
- The following code similarly corresponds to: y = K * x, but for what value of K?
x = 7
y = (x << 5) - (x << 2) + x;
- The following code corresponds to the mathematical equation: y = K * x, for some fixed value of K < 1. What is the value of K?
x = 35
y = (x >> 1) - (x >> 3)
- Relative to multiplication and division, what mathematical equation does the following code produce?
x = 102
y = x & 0x0f
- (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
- For the given values of a and b, what is printed?
- If the initial values of a and b were instead a = 4 and b = 17, what would be printed?
- How about if the initial values of a and b were a = 8 and b = 4 ?
- How about if the initial values of a and b were a = 5 and b = 25 ?
- 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?
- 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).
- (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.