Question: B) (4 Points) Consider a scenario where 4 signed bytes are stored into a 32-bit unsigned integer number. Remember, bytes within a word are numbered

B) (4 Points) Consider a scenario where 4 signed bytes are stored into a 32-bit unsigned integer number. Remember, bytes within a word are numbered from 0 (least significant byte or LSB) to 3 (most significant byte or MSB). Now consider the following function

: int extractSignedByte(unsigned word, int byte_number) {

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

}

This function should extract the designated byte and sign extends it to a 32-bit integer. However, a bug exists that prevents this function from performing the correct task. (1) (2 Points) What is the bug in this code and why do you think it can cause the function to return the wrong result, and (2) (2 Points) by using only left and right shifts, and one subtraction, replace the buggy code inside the extractSignedByte function with the correct implementation that will always cause the function to return the desired signed byte as a signed 32-bit integer. Please explain your implementation in few words. Hint: remember that right shifts of unsigned numbers are performed logically and right shifts of signed numbers are performed arithmetically. Refer to Practice Problem 2.23 in the book for additional help

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!