title: Transforming byte values so that their MSB is removed / restored
body: <p>I have a binary protocol which extracts the MSB of each payload byte into a MSB collection byte (septett) for transmission, and re-injects the MSBs on receiver side. The payload consists of n four byte frames, depending on sender (six in my case).</p>
  
  <p>Those are two example frames, with their septett (last byte), as seen on the wire:</p>
  
  <pre><code>0x2E 0x00 0x5F 0x00 0x04
  0x79 0x01 0x38 0x22 0x04
  </code></pre>
  
  <p>Those are the same frames, client side, with the MSBs re-injected:</p>
  
  <pre><code>0x2E 0x00 0xDF 0x00
  0x79 0x01 0xB8 0x22
  </code></pre>
  
  <p>The C functions that do the transformation are defined on pages 9 and 10 in <a href="https://docs.google.com/file/d/0B1ga9YrTFDiVZWU3NjBiNmYtZDczNS00NjE1LTk1ZDAtZmJhNWVkMmI5ZTRj/edit?hl=en&amp;authkey=CIzfn4UM&amp;pli=1" rel="nofollow">this document</a>. My version of these, in Java, is below. The problem I have is that none of this works, and I'm confused as to why. I pass my four bytes from the wire, and get same bytes out, untouched. I could use some help figuring out what's wrong here (probably something trivial that I fail to see).</p>
  
  <pre><code>private static byte[] vbusExtractSeptett(byte[] data, int offset, int length) {
      byte septett = 0;
      for (int i = 0; i &lt; length; i++) {
          if ((data[offset + i] &amp; 0x80) != 0) {
              data[offset + i] &amp;= 0x7F;
              septett |= 1 &lt;&lt; i;
          }
      }
  
      data[offset + length] = septett;
      return data;
  }
  
  private static byte[] vbusInjectSeptett(final byte[] data, int offset, int length) {
      byte septett = data[offset + length];
  
      for (int i = 0; i &lt; length; i++) {
          if ((septett &amp; (1 &lt;&lt; i)) != 0)
              data[offset + i] |= 0x80;
      }
  
      return data;
  }
  </code></pre>