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
Ethan is responsible for writing the dialogue in a movie. Which role is he playing? Ethan is playing the role of a(n).....''BLAN
iVinArrow [24]
Screenwriter! A screenwriter's role is to pen the script for a film.
3 0
3 years ago
Why is Net WPM a better formula than Gross WPM to calculate typing speed?
Lelu [443]

Answer:

I think it is because a gross is equal to 144 and that is a hard way to count

Explanation:

7 0
2 years ago
Which tools allows you to see the edition, version and memory use by windows 10?
cricket20 [7]

Answer:

Resource Monitor

Performance Monitor

Computer Management and Administrative Tools

Advanced User Accounts Tool

Disk Cleanup

Local Group Policy Editor

There is alot do u want them all?

6 0
3 years ago
An “AI” (artificial intelligence) could be used in:
zhannawk [14.2K]
I would say D. But l may be incorrect. Al bots are not made for games, but are made for information.

Sorry l can't be much of help, but l do hope that I did help you out in a way.
8 0
3 years ago
Which of these number formats would you want to apply to a cell showing the total sales for the month? Currency, Number, or Perc
ollegr [7]

Answer:

Currency

Explanation:

Since sales are in money, and currency shows money, Currency is our answer

3 0
3 years ago
Read 2 more answers
Other questions:
  • With a(n) ____ data table you can vary the value in one cell.
    5·1 answer
  • what is the restaurant with the black pom tree and yellow backround three letter name from hi guess the restaurant
    14·2 answers
  • When was unicode invented?
    13·1 answer
  • What were the technological innovations that allowed for the rapid expansion of the railroads?
    6·1 answer
  • Well I am having trouble and I feel really bad because I helped RandomGuy1 who rubs it in my face that I gave him the wrong answ
    15·1 answer
  • A user wants to make sure he can quickly restore his computer after a drive failure to the state it was in when Windows and all
    9·1 answer
  • What is the "online disinhibition effect"?​
    7·1 answer
  • A robot worker and a human worker are both vulnerable to
    5·1 answer
  • A file named "games.txt" exists and has 80 lines of data. You open the file with the following line of code.
    7·2 answers
  • __________ are very simple devices that connect network components, sending a packet of data to all other connected devices.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!