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]
3 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]3 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
Rachelle's computer has frequent system crashes and it takes a long time to access files and folders. What hardware component is
9966 [12]
System crashes, viruses and fragmented file systems are hallmarks of Windows systems, but your teacher is probably looking for Hard Drive for the answer.
8 0
3 years ago
What are Three types of informational references
Irina-Kira [14]

Answer:

There are many types of information al references such as Encyclopedias, dictionaries, thesaurus Almanacs, atlases, thesauruses, Atlases, almanacs, and encyclopedias.

Explanation:

There are also informational websites. The way to find this is to look at the website url to see if it ends in .gov, .edu, and .org . But make sure you cite your source so you don't plagiarize.

If you don't have any questions feel free to ask in the comments.

3 0
3 years ago
Read 2 more answers
How to transfer photos from iphone to iphone?
STALIN [3.7K]
If you have icloud then you can transfer them.
7 0
3 years ago
Read 2 more answers
How to improve and create beautiful graphic
kaheart [24]

Answer:

Learn OpenGL and any graphics rendering program.

Explanation:

3 0
3 years ago
When you save a Notepad file for the first time, you must also A. print a copy for your records. B. give it a name. C. check the
Kruka [31]
Answer: Give it a name
3 0
3 years ago
Other questions:
  • What is the maximum number of communication paths for a team of twenty people?
    5·1 answer
  • You order a new personal computer. A representative from the store calls to ask if you need CD-copying software in your personal
    12·1 answer
  • Which of the following statements is true?
    13·1 answer
  • What will be the output of the following code? &lt;?php $foo = 'Bob'; $bar = $foo; $bar = "My name is $bar"; print $bar; print $
    8·2 answers
  • Write the definition of a function reverse, whose first parameter is an array of integers and whose second parameter is the numb
    15·1 answer
  • Cuantos MB son 8,192 kb?​
    13·1 answer
  • What is command is used to improve the vocabulary and in which tab is it found​
    14·1 answer
  • Can someone please explain to me how the points work when you ask a question? Worth 30 points I think, I still don't know how th
    5·2 answers
  • PLEASE ANSWER AND HURRY I'LL MARK YOU BRAINLIEST!!<br><br><br>​
    9·1 answer
  • In how many positions are there nucleotide differences between your query sequence and the sequence of accession AY259214.1
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!