Home > Mobile >  Can someone explain these questions on shifting right arithmetic or shifting left logical?
Can someone explain these questions on shifting right arithmetic or shifting left logical?

Time:11-20

This was on an exam and I'll admit that I had no idea what was going on, shifts weren't discussed prior to the exam. They're both starting in hexadecimal and then the instructions shift one to the left logical and the other to the right arithmetic. For some reason the left logical one has every number plus 5 and then there's an 8 at the beginning, making the hex go from 7's to B's (12), and in the second one there's an E in the front but the numbers stay the same? My notes have the professor saying add two 0's or two 1's but I'm still not seeing where he got the answers from. The $t1 and $t0 values are given at the top and then below for each question is a different MIPS32 statement; sll $t2, $t1, 3 and sra $t2, $t0, 2. $t1 is in hex (0x7777 77777) and $t0 is (0x8888 88888).

enter image description here

CodePudding user response:

For starters, B is 11 so it's not 7 5. Also, you seemed to be doing addition, not shifting.

First convert the number to binary. Shifting left brings in zeroes from the right, so append 3 zeroes and chop off 3 leading bits from the front. Convert the number back to hex. You will see all the digits except the last get turned to B (because they pull in bits of the following 7). The last digit obviously has the 3 zeroes you appended so that ends up being 8.

Arithmetic shift right duplicates the most significant bit. In your case that's a 1, so prepend two 1 bits and chop off two bits from the right. While that does give 1110=E at the front, the rest of the digits will be 0010=2 not 8. The correct result is E2222222.

  • Related