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

2.Write code for the function with the following prototype: Assumptions Integers

ID: 3663842 • Letter: 2

Question

2.Write code for the function with the following prototype:

Assumptions

Integers are 64-bits long and represented in two’s-complement form.

Right shifts of signed data are performed arithmetically.

Forbidden

Casting, either explicit or implicit.

Relative comparison operators (<, >, <=, and >=).

Division, modulus, and multiplication.

Conditionals(if or ? :), loops, switch statements, function calls, and macro invocations.

Casting, either explicit or implicit.

Legal Operations

Each problem has a list of which operations you are allowed to utilize

Explanation / Answer

int problem2(long int x, long int n) {
/*
* Use left shift then right shift
* to sign extend from n bits to full int
*/

long int count = (sizeof(long int)<<3)-n;
long int leftright = (x << count) >> count;

/* See if still have same value */
cout<<"x: "<<x<<endl;
cout<<"left: "<<leftright<<endl;
return x == leftright;

}