▾ Part A - The effect of an arithmetic shift on signed numbers Let's look at see what happens to signed numbers during a shift operation. As with most problems with number representations, errors can be intermittent. Sometimes the code will work as expected, and other times it will behave in a manner that seems to be totally arbitrary. Consider the following code fragment that makes use of the fact that shifting a value left by one place multiplies the number by 2. By passing in the number to be multiplied and the power of 2 to multiply it by (for example 4 = 22) the correct answer should be passed back from the function after the results are printed for the user to examine signed int mult_2_to_n (signed int num, int n) ( signed int result; result = num << n; printf("%d multiplied by 2^d = $d\n", num, n, result); return result; } Using a signed number as the manipulated integer may cause an error in some cases. Several approaches may be used to fix the problem. Which solutions below are valid? (Assume that the return type will be modified as needed, and that there will be no unintended bits lost due to left-shiting) Select all answers that apply. There will be at least one correct answer. ▸ View Available Hint(s) saving the signed bit and restoring it at the end of the shift Ochanging the signed int to a representation that can be preserved □ shifting num to the right in the return statement Oshifting the signed bit to the left one more time □ checking that the shift is small enough not to interfere with the signed bit Submit Request Answer Activate Windows Go to Settings to activate Windows.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.4: A Case Study: Rectangular To Polar Coordinate Conversion
Problem 9E: (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by...
icon
Related questions
Question
▼
Part A - The effect of an arithmetic shift on signed numbers
Let's look at see what happens to signed numbers during a shift operation.
As with most problems with number representations, errors can be intermittent. Sometimes the code will work as expected, and other times it will behave in a manner that seems to be totally arbitrary. Consider the following code fragment that makes use of the fact that shifting a
value left by one place multiplies the number by 2. By passing in the number to be multiplied and the power of 2 to multiply it by (for example 4 = 22) the correct answer should be passed back from the function after the results are printed for the user to examine.
signed int mult_2_to_n (signed int num, int n)
{
signed int result;
result = num << n;
printf("%d multiplied by 2^%d %d\n", num, n, result);
return result;
}
Using a signed number as the manipulated integer may cause an error in some cases. Several approaches may be used to fix the problem. Which solutions below are valid? (Assume that the return type will be modified as needed, and that there will be no unintended bits lost
due to left-shiting).
Select all answers that apply. There will be at least one correct answer.
► View Available Hint(s)
saving the signed bit and restoring it at the end of the shift
changing the signed int to a representation that can be preserved
Oshifting num to the right in the return statement
Oshifting the signed bit to the left one more time
Ochecking that the shift is small enough not to interfere with the signed bit
Submit
Request Answer
Activate Windows
Go to Settings to activate Windows.
Transcribed Image Text:▼ Part A - The effect of an arithmetic shift on signed numbers Let's look at see what happens to signed numbers during a shift operation. As with most problems with number representations, errors can be intermittent. Sometimes the code will work as expected, and other times it will behave in a manner that seems to be totally arbitrary. Consider the following code fragment that makes use of the fact that shifting a value left by one place multiplies the number by 2. By passing in the number to be multiplied and the power of 2 to multiply it by (for example 4 = 22) the correct answer should be passed back from the function after the results are printed for the user to examine. signed int mult_2_to_n (signed int num, int n) { signed int result; result = num << n; printf("%d multiplied by 2^%d %d\n", num, n, result); return result; } Using a signed number as the manipulated integer may cause an error in some cases. Several approaches may be used to fix the problem. Which solutions below are valid? (Assume that the return type will be modified as needed, and that there will be no unintended bits lost due to left-shiting). Select all answers that apply. There will be at least one correct answer. ► View Available Hint(s) saving the signed bit and restoring it at the end of the shift changing the signed int to a representation that can be preserved Oshifting num to the right in the return statement Oshifting the signed bit to the left one more time Ochecking that the shift is small enough not to interfere with the signed bit Submit Request Answer Activate Windows Go to Settings to activate Windows.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr