Question
#include #include #include #include #include q5.h int saturating_add(int x, int y); { //Solution needed here. } //////////////////////////////////////////////////////////////// // test code void test_sat_1_plus_1() { assert(saturating_add(1,1) ==
#include
#include "q5.h"
int saturating_add(int x, int y);
{
//Solution needed here.
}
//////////////////////////////////////////////////////////////// // test code void test_sat_1_plus_1() { assert(saturating_add(1,1) == 2); } void test_neg_ov() { assert(saturating_add(INT_MIN, -5) == INT_MIN); assert(saturating_add(INT_MIN, INT_MIN) == INT_MIN); } void test_pos_ov() { assert(saturating_add(INT_MAX, 1) == INT_MAX); } void test_big_pos_ov() { assert(saturating_add(INT_MAX, INT_MAX) == INT_MAX); } int main() { std::cout
5. (5 marks) 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 twos-complement addition does, satu- rating addition returns INT_MAX when there would be positive overflow, and INT_MIN when there would be negative overflow. Saturating arithmetic is com- monly used in programs that perform digital signal processing. Your function should follow the bit-level integer coding rules above
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started