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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started