# Assignment 2 Solution

\$30.00

Category:

## Description

Please submit individual source files for coding exercises (see naming conventions below) and a single solution document for non-coding exercises (.txt or .pdf only), when appropriate. Your code and answers need to be documented to the point that the graders can understand your thought process. Full credit will not be awarded if sufficient work is not shown.

1. [20] Suppose we number the bits in a 32-bit word from 0 (least significant) to 31 (most significant). Write code for the following C function that will return a bit mask containing 1s for the least-significant n bits and 0s for the remaining most-significant bits:

Your solution will need to handle the case that mask is called with input 32 (hint: shifting a 32-bit word by 32 in either direction is undefined in standard C, so don’t do it; another hint: the int return type can be exploited to handle this case).

Here are some test runs:

Use only bitwise operators and subtraction; no if statements, loops, or other arithmetic operators (+, *, /, %). Also write a main() function to test your function. Name your source file 2-1.c

1. [20] Suppose we number the bytes in a 32-bit word from 0 (least significant) to 3 (most significant) and that the word consists of 4 individual signed bytes. Write code for the following C function that will return byte i of x sign extended to 32 bits:

unsigned int extract (unsigned int x, int i);

Here are some test runs:

extract(0x12345678, 0): 0x00000078

extract(0xABCDEF00, 2): 0xFFFFFFCD

Use only bitwise operators and subtraction; no if statements, loops, or other arithmetic operators (+, *, /, %). Also write a main() function to test your function. Name your source file 2-2.c

[15] Fill in the missing expression in the following C code such that it will return 1 if x is >= y,

• otherwise (you can assume that neither argument is NaN and that +0 and -0 are considered equal):

 int ge(float x, float y) { unsigned ux = *((unsigned *) &x); // convert x raw bits unsigned uy = *((unsigned *) &y); // convert y raw bits unsigned sx = ux >> 31; // extract sign bit of ux unsigned sy = uy >> 31; // extract sign bit of uy

ux <<= 1; // drop sign bit of ux

uy <<= 1; // drop sign bit of uy

// TODO: return using sx, sy, ux, uy

}

Here are some test runs:

ge(0.0f, 0.0f): 1

ge(-0.0f, 0.0f): 1

ge(-1.0f, 0.0f): 0

ge(0.0f, 1.0f): 0

ge(1.0f, 0.0f): 1

ge(0.0f, -1.0f): 1

Use only bitwise operators; no if statements, loops, or arithmetic operators (+, -, *, /, %). Also write a main() function to test your function. Name your source file 2-3.c

1. [15] Convert the following hex values to decimal assuming that they are stored as 2s complement integers.

1. (5) 0x000000FF

1. (5) 0xFFFFFCE6

1. (5) 0xFFFFFFFF

1. [15] Convert the following hex values to decimal assuming that they are encoded as IEEE 754 single-precision floating-point numbers:

1. (5) 0x80000000

1. (5) 0x41220000

1. (5) 0xC39D0000

6 [15] Convert the following decimal numbers to hex encoded as IEEE 754 single-precision floating-point numbers. Write your answers in your solutions document.

1. (5) 1.0

1. (5) 8.25

1. (5) -25.125