title: How do I efficiently reverse the last seven bits of a 16-bit integer in MIPS?
body: <p>For example, if I have an integer that can be represented as 01234567, then I need to reverse it and preserve the first bit so it looks like 07654321. I also need to do this as efficiently as possible. </p>
  
  <p>I already found this online:</p>
  
  <pre><code>for (x=0;x&lt;8;x++) {
  bit = input &amp; 0x01; // get the low bit
  output |= bit; // set the low bit in output
  input &gt;&gt; 1; // shift to the right for AND mask
  output &lt;&lt; 1; // move all bits to the left, low bit = 0 now
  }
  </code></pre>
  
  <p>And to implement that (plus keeping the first bit at the beginning) I wrote this:</p>
  
  <pre><code>    andi $7, $8, 0xC000     #$7: obtain the first two bits that we want to save
      sll $7, $7, 2           #$7: sll to make it possible to later append $7 on to the beginning
  
  Flip:andi $4, $8, 0x03      #$4: bitwiseAND the reference with 0x03 to isolate last two bits
      or $5, $5, $4           #$5: store the bit using bitwiseOR 
      sll $5, $5, 2           #$5: sll $5 to create space for the next two bits to be loaded in 
      srl $8, $8, 2           #$2 slr $8 so the next two bits can be isolated
      addi $13, $13, -1       # decrease counter
      bne $13, $0, Flip       # if $13 is not equal to zero, then branch to start of Flip 
  
      or $5, $5, $7           # add the saved bit back onto the beginning 
      srl $5, $5, 2           # srl to get rid of the execess zeros at the end
  </code></pre>
  
  <p>(After looking at other questions here, I realize my format is different than the norm - this is just how I was taught in class)</p>
  
  <p>That code works, but I'm looking for a more efficient way to implement it. Can anyone offer guidance on how to improve this?</p>