Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sra(shift right arithmetic) vs srl (shift right logical)

Please take a look at these two pieces of pseudo-assembly code:

1)

li $t0,53

sll $t1,$t0,2
srl $t2,$t0,2
sra $t3,$t0,2

print $t1  
print $t2  
print $t3  

2)

li $t0,-53


sll $t1,$t0,2
srl $t2,$t0,2
sra $t3,$t0,2

print $t1
print $t2
print $t3

in the first case the output is:
212
13
13

in the latter is:
-212
107374...
-14
But shouldn't : sra (-53) = - (srl 53) ?

like image 397
user591931 Avatar asked Jun 07 '11 17:06

user591931


People also ask

What is the difference between logical shift right and arithmetic shift right?

Logical right shift means shifting the bits to the right and MSB(most significant bit) becomes 0. Example: Logical right shift of number 1 0 1 1 0 1 0 1 is 0 1 0 1 1 0 1 0. Arithmetic right shift means shifting the bits to the right and MSB(most significant bit) is same as in the original number.

How are instructions SRA & Srl different?

Shifting is to move all the bits in a register left or right. sll/srl mean shift left/right logical while sra means shift right arithmetic for which the sign-bit (rather than 0) is shifted from the left as illustrated in Figure 3.2.

What is the difference between arithmetic shift and logical shift operation?

Arithmetic shift perform multiplication and division operation, whereas Logical shift perform only multiplication operation. Arithmetic shift is used for signed interpretation, whereas Logical shift is used for unsigned interpretation.

What is a logical shift right?

MIPS also has a shift right logical instruction. It moves bits to the right by a number of positions less than 32. The high-order bit gets zeros and the low-order bits are discarded. If the bit pattern is regarded as an unsigned integer, or a positive two's comp.


2 Answers

-53 = 1111111111001011

           sra 2

      1111111111110010(11) = -14
       ^^              ^^
      sign           dropped
    extension

Because the extra bits are simply dropped for both positive and negative results, the result is always rounded down if you view the shift as a division.

 53 sra 2 = floor( 53 / 2^2) = floor( 13.25) =  13
-53 sra 2 = floor(-53 / 2^2) = floor(-13.25) = -14
like image 116
ikegami Avatar answered Sep 19 '22 13:09

ikegami


The answer relates to two's complement notation. The purpose of sra is to support negative numbers represented in two's complement. The most significant bit, which is one if the value is negative, is duplicated when shifted right in the "arithmetic" fashion.

On your 32-bit x86, this means that:

 53 = 00000000000000000000000000110101
-53 = 11111111111111111111111111001011

 srl( 53, 2) =  13 = 00000000000000000000000000001101
               -13 = 11111111111111111111111111110011

 sra(-53, 2) = -14 = 11111111111111111111111111110010

I suppose the thing to realize is that in two's complement, the negative of a number is not the inversion of every bit in the number -- it is the inversion of every bit, then the addition of 1 to that number. Consider:

 1 = 0000001
-1 = 1111111

Not:

-1 = 1111110

Which would lead to:

 0 = -1 + 1 = 11111111

In other words, there is no "negative zero" in two's complement. Zero takes up space in the realm otherwise considered "positive sign" because the high bit is zero.

like image 40
Heath Hunnicutt Avatar answered Sep 23 '22 13:09

Heath Hunnicutt