Question

Can someone please explain the logic behind the function /* * logicalShift - shift x to...

Can someone please explain the logic behind the function

/*

* logicalShift - shift x to the right by n, using a logical shift

* Can assume that 0 <= n <= 31

* Examples: logicalShift(0x87654321,4) = 0x08765432

* Legal ops: ! ~ & ^ | + << >>

* Max ops: 20

* Rating: 3

*/

int logicalShift(int x, int n) {

//use mask to ignore the leading bits

int mask = ~((1 << 31) >> n << 1);

return (x >> n) & mask;  

}

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Can someone please explain the logic behind the function /* * logicalShift - shift x to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for Data Labs ( C Programming with only <limits.h> for library) /* * logicalShift...

    This is for Data Labs ( C Programming with only <limits.h> for library) /* * logicalShift - shift x to the right by n, using a logical shift * Can assume that 1 <= n <= 31 * Examples: logicalShift(0x87654321,4) = 0x08765432 * Legal ops: ~ & ^ | + << >> * Max ops: 16 * Rating: 3 */ int logicalShift(int x, int n) { return 2; } For every line of code please give me a descriptive explanation...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • Question 4 (2 points) Consider the C statements below. Assume x has been assigned a value....

    Question 4 (2 points) Consider the C statements below. Assume x has been assigned a value. int mask = 0xFF000000; y = x ^ mask; Which one of the following best describes the value computed for variable y? Question 4 options: y contains the most significant byte of x complemented, with the rest of the bits unchanged y contains the most significant byte of x unchanged, with the rest of the bits 0 y contains the most significant byte of...

  • Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x....

    Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x. Assume w=32. * For example 0xFF00 -> 0x8000, and 0x6600 --> 0x4000. * If x = 0,then return 0. */ int leftmost_one(unsigned x); Your function should follow the above bit-level integer coding rules, except that you may assume that data type int has w=32 bits. Your code should contain a total of at most 15 arithmetic, bit-wise, and logical operations. In C++ and has...

  • hi, can someone explain this method please: public static int sumCapped(int nums[] , int x) {...

    hi, can someone explain this method please: public static int sumCapped(int nums[] , int x) { int sum=0; if(nums.length==0) { return Integer.MAX_VALUE; } for(int i=0;i<nums.length;i++) { if(sum+nums[i]<=x) // check if adding the succssive element return gives a values less than or equal to x { sum=sum+nums[i]; // if true then add } } return sum; // return the output }

  • Can someone please explain to me this? I'm having a hard time understanding it. thank you...

    Can someone please explain to me this? I'm having a hard time understanding it. thank you 2:31 $32% X 324 325 14) Given the following function definition: void calc (int a, int& b) intc; C = a + 2; a = a* 3; b = c + a; What is the output of the following code fragment that invokes calc? int x = 1; int y = 2; int z = 3; calc(x,y); cout << x <<""<< y «""<< Z...

  • Can someone please explain how the middle expression ' tan^n-1x/n-1 ' is split? The entire right-hand...

    Can someone please explain how the middle expression ' tan^n-1x/n-1 ' is split? The entire right-hand side then becomes ' Int tan^n-2 x tan^2xdx ' I'm just not sure what principles/methods I'm using to split this? Thank you n-1 tan .X n-2 Stan"xdx ſtan xdx n-1

  • Write code for a function with the following prototype: // Divide by power of two. Assume...

    Write code for a function with the following prototype: // Divide by power of two. Assume 0 <= k < w-1 int divide_power2(int x, int k); The function should compute x/2 k with correct rounding (round toward 0), and it should follow the bit-level integer coding rules: • Assumptions – Integers are represented in twos-complement form. – Right shifts of signed data are performed arithmetically. – Data type int is w bits long. For some of the problems, you will...

  • can someone explain what is going on inside the strcut? c++ struct Pos{ int x; int...

    can someone explain what is going on inside the strcut? c++ struct Pos{ int x; int y; // coordinates Pos(const Pos &p, int dx=0, int dy=0){ *this = p; x+=dx; y+=dy;}    Pos(int _x, int _y){ x=_x; y=_y; } //new location    bool operator<(const Pos & p) const { return (x < p.x) || (x==p.x && y < p.y); }       bool operator==(const Pos & p) const { return x==p.x && y==p.y; } Pos(){x=-1;y=-1;} };

  • This is a PHP code. It is a project of roman converter Can someone explain this...

    This is a PHP code. It is a project of roman converter Can someone explain this statement to me? What does it mean? $romanValid = "/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/"; and in the second function, what does the first&neighbor stand for? can you explain the logic of this function for me, please?? thank you function validRoman($str) $romanValid = "/"M{0,4}(CM CD D2C{0,37) (XC XL L?X{0,33) (IX IVV?10,3)/": return (preg_match($romanValid, $str)); function RomanToNum($str) $first = 0; $neighbor = 0; $result = 0; $len = strlen($str); for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT