Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following functions in C whose signatures are provided below: Restrict

ID: 3877520 • Letter: W

Question

Write the following functions in C whose signatures are provided below:

Restrictions:

No relational operators like <=, >=, <, >, or pow() allowed, nor can there be special cases on bitwidth, as such no if switch ?: is allowed for special bitwidths, but LOGICAL operators/ conidtional statements are allowed especially to sort out over/underflow cases, == and != ARE ALLOWED as well.

An example:

The function allows two operands (or more) to be added together and if their values exceed the min/max for the provided bitwidth, the answer saturates at the respective min/max. Note 126+5 > 127, but it saturated at the maximum.

long signed_min(int bitwidth); long signed max(int bitwidth): long sat_add (long operand1, long operand2, int bitwidth);

Explanation / Answer

Note: From the theory we know that

The two's complement range with N digits is (2N1) to +(2N - 1 - 1)

Now this can be implemented through C function as

1. long signed_min(int bandwidth){

int result = 1; //initialize a variable with initial value 1.

while(bandwidth != 1){

result = result*2; // compute the minimum value for result

bandwidth--;

}

return result;

}

2. long signed_max(int bandwidth){

int result = 1;

while(bandwidth != 1){

result = result*2; //compute the maximum value for result

bandwidth--;

}

return result-1;

}

3. long sat_add(int operand1, int operand2, int bandwidth){

int sum = operand1 + operand2; // calculate sum for two operands passed in function

int signed_min_value = signed_min(bandwidth); // calculatte the lower range for given bandwidth of the function

int signed_max_value = signed_max(bandwidth); // calculate the higher range for the given bandwidth of the function

while((!(signed_max_value/sum ) ) || (!(signed_min_value/sum ) ){

sum--

}

/* In this while loop we check for the value greater than the higher range or lower than the lowest range for the given bandwidth of the sum value.   

return sum;

}

The two's complement range with N digits is (2N1) to +(2N - 1 - 1)