Answer:
See explaination
Explanation:
program code.
/* PRE PROCESSOR DIRECTIVES */
#include<stdio.h>
/* PRE-DEFINED VALUES FOR TMAX AND TMIN */
#define TMax 2147483647
#define TMin (-TMax -1)
/* saturating_add(int,int) METHOD IS CALLED HERE */
int saturating_add(int firstNumber, int secondNumber)
{
/*
FOR BETTER UNDERSTANDING, LETS TAKE TEST CASE,
WHERE firstNumber = 5 AND secondNumber = 10
*/
int w = sizeof(firstNumber) << 3;
/*
sizeof(firstNumber) VALUE IS 4, SO USING BINARY LEFT SHIFT OPERATOR TO THREE PLACES,
WE HAVE NOW VALUE 32, ASSIGNED TO w
*/
/* ADDITION IS CALCULATED => 15 */
int addition = firstNumber + secondNumber;
/*
MASK INTEGER VARIABLE IS TAKEN
mask BIT IS LEFT SHIFTED TO 31 PLACES => 2^31 IS THE NEW VALUE
*/
int mask = 1 << (w - 1);
/* FIRST NUMBER MOST SIGNIFICANT BIT IS CALCULATED BY USING AND OPERATOR */
int msbFirstNumber = firstNumber & mask;
/* SECOND NUMBER MOST SIGNIFICANT BIT IS CALCULATED BY USING AND OPERATOR */
int msbSecondNumber = secondNumber & mask;
/* MOST SIGNIFICANT BIT OF ADDITION IS CALCULATED BY USING AND OPERATOR */
int msbAddition = addition & mask;
/* POSITIVE OVERFLOW IS DETERMINED */
int positiveOverflow = ~msbFirstNumber & ~msbSecondNumber & msbAddition;
/* NEGATIVE OVERFLOW IS DETERMINED */
int negativeOverflow = msbFirstNumber & msbSecondNumber & !msbAddition;
/* THE CORRESPONDING VALUE IS RETURNED AS PER THE SATURATING ADDITION RULES */
(positiveOverflow) && (addition = TMax);
(negativeOverflow) && (addition = TMin);
return addition;
}
/* MAIN FUNCTION STARTS HERE */
int main(){
/* TEST CASE */
int sum = saturating_add(5, 10);
/* DISPLAY THE RESULT OF TEST CASE */
printf("The Sum Is : %d\n\n",sum);
}