Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

you just started working for a company that is implementing a set of procedures

ID: 3667737 • Letter: Y

Question

you just started working for a company that is implementing a set of procedures to operate on a data structure where 4 signed bytes are packed into a 32-bit unsigned. Bytes within the word are numbered from 0 (least significant) to 3 (most significant). You have been assigned the task of implementing a function for a following machine using two's-complement arithmetic and arithmetic right shifts with the following prototype:

/* Declaration of data type where 4 bytes are packed into an unsigned */

typedef unsigned packed_t;

/*extract byte from word. return as signed integer */

int xbyte(packed_t, word, int bytenum) {

return (word >> (bytenum << 3) & 0xFF;

}

A. what is wrong with this code?

B. give a correct implementation of the function that uses only left and right shifts, along with one subtraction

Explanation / Answer

A. return (word & (0xFF << (1 << bytenum))) >> (1 << bytenum)

B. int xbyte(packed_t word, int bytenum){

return (word & (0xFF << (1 << bytenum))) >> (1 << bytenum);

}