1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Diano4ka-milaya [45]
4 years ago
12

Write code for a function with the following prototype: /* Addition that saturates to TMin or TMax */ int saturating_add(int x,

int y); Instead of overflowing the way normal two's-complement addition does, saturating addition returns TMax when there would be positive overflow, and TMin when there would be negative overflow. Saturating arithmetic is commonly used in programs that perform digital signal processing. Your function should follow the bit-level integer coding rules (page 128).
Computers and Technology
1 answer:
Kazeer [188]4 years ago
7 0

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);

}

You might be interested in
The smallest network
katrin [286]

Smaller networks include LANs or MANs.

Hope this is what you were looking for!

3 0
3 years ago
Carlos is using the software development life cycle to create a new app. He has finished coding and is ready to see the output i
Nitella [24]

Answer:

Testing

Explanation:

From the question, we understand that Carlos just finished the coding of the app.

In software development life cycle, the coding phase is where Carlos is expected to make use of his choice of programming language to design the app;

This stage is an integral part of the implementation process and according to the question, the coding has been completed;

The next phase or stage after the implementation phase is testing.

Hence, Carlos is getting ready to test the app.

4 0
3 years ago
Read 2 more answers
What does it mean when #DIV/0! is displayed and a green triangle is added to the upper-left corner of a cell? A. The formula can
bazaltina [42]
The answer to the question is B.
4 0
4 years ago
Read 2 more answers
Marty uses a customized database to sort parts and track inventory. The customized database is an example of _____.
MakcuM [25]

cataloging, that is a term for it.

3 0
3 years ago
Alison is having a hard time at work because her Inbox is flooded with emails every day. Some of these emails are unsolicited. S
miskamm [114]

Personally I just leave them in my inbox because who cares, Alison should; however, move them to the trash I guess

7 0
3 years ago
Other questions:
  • A school librarian has been asked to identify inappropriate Internet usage. Which students would most likely be reported to the
    5·2 answers
  • in c++, what happends when i add 1 to the RAND_MAX then take rand() deivded by (RAND_MAX+1)?if my purpose is to take out a rando
    11·1 answer
  • Jorge needs to print out an essay he wrote but he does not have a printer his neighbor has a printer, but her internet connectio
    12·1 answer
  • Describing Editing Task
    6·1 answer
  • Object Oriented Programming (OOP) allows us to handle complex code in an organized structure and break down the problem by model
    5·1 answer
  • Understanding the context of information allows you to apply it to everyday situations as opposed to just___facts like names,dat
    12·1 answer
  • Below is a recursive implementation of the factorial function. For what value of n will this function not produce the desired re
    8·1 answer
  • What information should the subtitles in the clips of your video per folio provide?
    14·1 answer
  • The term used to describe the shape and layout of a computer component such as a motherboard or hard drive is ______ factor.
    5·1 answer
  • - A blacksmith is shoeing a miser's horse. The blacksmith charges ten dollars for his work. The miser refuses to pay. "Very well
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!