-2

So when shifting -34 by 2 bits using SRA, I get an output of -9 using the code below. I can not figure out why it is giving me -9 and not another number

addi $t0, $zero, -34
sra $s0, $t0, 2
addi $v0, $zero, 4
la $a0, result1
syscall
addi $v0, $zero, 1
move $a0, $s0
syscall
Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
  • floor(-34/4) is -9. Arithmetic right shift rounds towards -inf. – Peter Cordes Apr 30 '21 at 07:25
  • Does this answer your question? [Which kind of signed integer division corresponds to bit shift?](https://stackoverflow.com/questions/63018450/which-kind-of-signed-integer-division-corresponds-to-bit-shift) – Peter Cordes Apr 30 '21 at 07:31

1 Answers1

1

-34 in (two's complement) binary is:

11111111 11111111 11111111 11011110
^                                 ^
bit 31                            bit 0

Shift that arithmetically 2 bits to the right and you get:

11111111 11111111 11111111 11110111

Which is -9

Michael
  • 55,116
  • 9
  • 74
  • 120