Question

In: Computer Science

PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP For the following C functions: int...

PLEASE ANSWER I WILL RATE YOUR ANSWER AND THUMBS UP

For the following C functions:

int q7(int x) {

    return x & (~x+1);

}

int q8(int x, int m, int n) {

    int a = ~m+1;

    int b = ~x +1;

    a = x + a;

    b = b + n;

    return !((a|b) >> 31);

}

int q9(int x, int n) {

   /* assume x and n are not a negative integer */

    int temp = (1 << n);

    int z = temp + ~0;

    return (z & x);

}

---------------

Part i) Explain what each function does without running the code. EXPLAIN YOUR ANSWER OR YOU WILL NOT RECEIVE CREDIT.

Part ii) For each of the above functions, write a simpler version of the function (if there is one)

Solutions

Expert Solution

Let us analyze each of the fuction,

Given function,

A)

int q7(int x) {

    return x & (~x+1);

}

i) Explaination: Given function computes bitwise and with its compliment plus one, and therefore returns 1 if least significiant bit 1, else return 2 for least significiant bit 0.

ii) above function already in simpler form.

B)

int q8(int x, int m, int n) {

    int a = ~m+1;

    int b = ~x +1;

    a = x + a;

    b = b + n;

    return !((a|b) >> 31);

}

i) Explaination: given function q8 takes 3 input parameter and computes bitwise compliment for first and second integer bit + 1, and add integer first integer with computed second bitwise integer store as int a, and second with third integer store as b, returns compliment of bitwise a or b shifting 31 bits to right. in simpler words we simply divide a | b with 2, 31 times, so function already in simpler form.

C)

int q9(int x, int n) {

   /* assume x and n are not a negative integer */

    int temp = (1 << n);

    int z = temp + ~0;

    return (z & x);

}

i) Explaination: function q9 takes two integer, shift bitwise left which means multiplying with 2 second integer n and store in temp, integer z stores temp subtracted with one, since bitwise compliment of 0 returns -1, finally return bitwise and of z and first integer x parameter.

In simpler words always integer second parameter or n value will be returned.

ii) More simplified version:

int q9(int x, int n) {
        /* assume x and n are not a negative integer */
        return n;

}

Related Solutions

PLEASE SOLVE I WILL THUMBS UP AND RATE YOUR ANSWER WELL For the following two C...
PLEASE SOLVE I WILL THUMBS UP AND RATE YOUR ANSWER WELL For the following two C functions: int q1(int x) { int m8 = 0x55; int m16 = m8 | m8 << 8; int m32 = m16 | m16 <<16; int z = x | m32; return !(~z); } int q2(int x) { int m8 = 0x55; int m16 = m8 | m8 << 8; int m32 = m16 | m16 <<16; int z = x & m32; return !!z;...
PLEASE SOLVE I WILL RATE AND THUMBS UP Assembly code question Write an LC3 program to...
PLEASE SOLVE I WILL RATE AND THUMBS UP Assembly code question Write an LC3 program to compute the XOR (exclusive OR) operation. The program computes the XOR of two numbers stored in registers R0 and R1 and returns the result in register R3. To test your program, before computing the XOR load the two values from memory locations x4000 (into R0) and x4001 (into R1).
Please answer all of the following for thumbs up: Explain the problem of intellectual property theft....
Please answer all of the following for thumbs up: Explain the problem of intellectual property theft. Next, address the reasonable and customary processes and procedures which should be used to discourage or make it difficult for employees, managers, and executives to inadvertently misuse and/or steal the company's intellectual property (at a minimum, you must address data classification and marking, separation of duties, and least privilege). You should also identify and explain five or more best practices which the company should...
This is for ethics. PLEASE DO NOT ANSWER IF YOU ARE NOT SURE. I WILL THUMBS...
This is for ethics. PLEASE DO NOT ANSWER IF YOU ARE NOT SURE. I WILL THUMBS DOWN IF INCORRECT. ANSWERS MUST BE CORRECT. T/F QUESTIONS PLEASE PROVIDE SOURCES. . The crime control paradigm of policing is most consistent with utilitarianism 7. One of the key principles of Packers crime control model is that repression of criminal behavior is more important than procedural justice. 8. The crime control model is consistent with the role of police officers as public servants.
Please answer following BOTH questions for THUMBS UP:- 1) Taking the Roman Coliseum as an example,...
Please answer following BOTH questions for THUMBS UP:- 1) Taking the Roman Coliseum as an example, describe how modern cost management accounting could be applied to a current attempt to recreate this structure. Be creative in your discussion. Add images to your answer. 2) How can budgeting be viewed as a choice process? Find a cartoon/comic strip image on the Internet to illustrate your discussion. Explain the relevance of this image to your answer. Find and use at least one...
Please answer this question correctly and quickly for a thumbs up. In the Molly Anderson article...
Please answer this question correctly and quickly for a thumbs up. In the Molly Anderson article assigned for this class, Professor Anderson describes her food systems vision. Thinking about your own food systems vision, what are three things you would like to change or affect in the food system? Please respond by listing one thing you would like to do individually, one thing you would like society to do collectively, and one thing you think the UW could do to...
What is better, BMW or Mercedes in your opinion? Thumbs up for every answer!
What is better, BMW or Mercedes in your opinion? Thumbs up for every answer!
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3] = {1, -2, 15 }; int result = 0, index = 0; int numElements = 3; while (index < numElements) { if ( index >= 1 && numberArray[index] > 3 ) { result += numberArray[index] ; } else { result -= 3; } index++; } return result; } int problem2_ ( ) { int a, modulo int answer = 0, b = 3; for...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3]...
Change the following C++ functions to Assembly x86: int problem1_ ( ) { int numberArray [3] = {1, -2, 15 }; int result = 0, index = 0; int numElements = 3; while (index < numElements) { if ( index >= 1 && numberArray[index] > 3 ) { result += numberArray[index] ; } else { result -= 3; } index++; } return result; } int problem2_ ( ) { int a, modulo int answer = 0, b = 3; for...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200 words or more, please explain why angel investments are important. *NOTICE* I do NOT need a definition of what angel investors are, I just need to know why angel investing is important
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT