# CIS 314 Assignment 2

\$30.00

## Description

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
2. [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
3. [15] Fill in the missing expression in the following C code such that it will return 1 if x is >= y,
0 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