Explanation of Solution
Function definition for “mul3div4()” function:
The implementation for “mul3div4()” function is given below:
//Header file
#include <stdio.h>
#include <assert.h>
#include <limits.h>
//Function definition for divide by power of "2"
int divide_power2(int x, int k)
{
//Determine negation value
int isNegation = x & INT_MIN;
//Check condition
(isNegation && (x = x + (1 << k) - 1));
//Returns value by using "x >> k" that is "x/(2^k)"
return x >> k;
}
//Function definition for mul3div4 function
int mul3div4(int x)
{
//Compute multiply "3"
int m3 = (x << 1) + x;
/* Returns the value of "3 * x/4" by calling function "divide_power2" */
return divide_power2(m3, 2);
}
//Main function
int main(int argc, char* argv[])
{
//Assign the value for "x"
int xValue = 0x87651234;
/* Call function "mul3div4" with checking "x * 3 / 4" value using assert function */
assert(mul3div4(xValue) == xValue * 3 / 4);
return 0;
}
The given code is used to compute the value of “
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Write code for a function (C++) with the following pro- totype: /* Addition that saturates to TMin or TMax */ int16_t saturating_add(int16_t x, int16_t y); Instead of overflowing the way normal two’s-complement addition does, satu- rating 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 from as1.arrow_forwardDefine a C++ function resetBit that takes two integer variables n, i and resets the ith bit of the number n and returns the number n from the function.arrow_forwardI want to write C or C++ functions that evaluate to ONE when the given conditions are true, and to ZERO when they are false. The following are the fourt conditions: int a(int x); //Any bit of x equals 0. int b(int x); // Any bit of x equals 1. int c(int x); //Any bit in the LSB of x equals 0. int d(int x); //Any bit in the MSB of x equals 1. The code should follow the bit-level integer coding rules, with the additional restriction that you may not use equality (==) or inequality (!=) tests.arrow_forward
- Suppose you are looking for duplication among n numbers between 1 and 366 that are provided as input. How would you approach this issue?What is the growth rate of the function T(n), which describes how long it takes you to find duplicates? (Hint: Select the proper n0.)arrow_forwardIn C, write a function int setbit(int n, int i) to set the i^ᵗʰ bit of n if i^ᵗʰ bit is 0.arrow_forward// Task 3 // For this function, you must return the largest power of 2 that // is less than or equal to x (which will be positive). You may // not use multiplication or some sort of power function to do this, // and should instead rely on bitwise operations and the underlying // binary representation of x. If x is 0, then you should return 0. unsigned largest_po2_le(unsigned x) { return x; }arrow_forward
- I could use some help converting this problem into code for IA32 x86 Assembly floatIsEqual - Compute f == g for floating point arguments f and g. Both the arguments are passed as unsigned int's, but they are to be interpreted as the bit-level representations of single-precision floating point values. If either argument is NaN, return 0 +0 and -0 are considered equal.arrow_forwardI need a code which finds the zeros and poles of this transfer function below in Python. Help? H = 10*(s+1)/(s+3)**2arrow_forwardThis exercise is about the bit-wise operators in C. Complete each functionskeleton using only straight-line code (i.e., no loops, conditionals, or functioncalls) and limited of C arithmetic and logical C operators. Specifically, youare only allowed to use the following eight operators: ! ∼, &,ˆ|, + <<>>.For more details on the Bit-Level Integer Coding Rules on p. 128/129 of thetext. A specific problem may restrict the list further: For example, to writea function to compute the bitwise xor of x and y, only using & |, ∼int bitXor(int x, int y){ return ((x&∼y) | (∼x & y));}(a) /*copyLSbit:Set all bits of result to least significant bit of x* Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000* Legal ops: ! ∼ & ˆ| + <<>>*/int copyLSbit(int x) {return 2;}(b) /* negate - return -x* Example: negate(1) = -1.* Legal ops:! ∼ & ˆ| + <<>>*/ int negate(int x) { return 2; }(c) /* isEqual - return 1 if x == y, and 0 otherwise*…arrow_forward
- Implement the following error detection and error correction algorithms using C programming for Hamming code. For Hamming codes, flip a bit in the data and implement the algorithm to correct the same on the receiver side.arrow_forwardConsider the following code which includes a parity check for the third digit: C=(000,011,101,110). You get the third digit by adding the first two and then using the remainder on division by 2. Can this code detect and count single errors? how can this be implemented?arrow_forwardSuppose we number the bytes in a w-bit word from 0 (least significant)to w/8 −1 (most significant).Write code for the following C function, which will return an unsignedvalue in which byte i of argument x has been replaced by byte b:unsigned replace byte (unsigned x, int i, unsigned char b);Examples:replace byte(0x12345678, 2, 0xAB) → 0x12AB5678replace byte(0x12345678, 0, 0xAB) → 0x123456ABarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr