title: swapping the order of a multibyte
body: <p>I have the following multi-byte represented in hex format:</p>
  
  <pre><code>0xdc, 0xd3
  </code></pre>
  
  <p>I can process the bytes in little endian () format to get the decimal value 54236:</p>
  
  <pre><code>List&lt;Integer&gt; packet = new LinkedList&lt;Integer&gt;(Arrays.asList(0xdc, 0xd3 ));
  int idx=0;
  int rpm = (int)readBytes(packet, idx, 2);
  
  private long readBytes(List&lt;Integer&gt; packet, int idx, int size){
      long val=0;
      int element;
  
      for(int i=0;i&lt;size;i++, idx++){
          element = packet.get(idx);
          val |= element &lt;&lt; (8 * i);
      }
  
      return val;
  }
  </code></pre>
  
  <p>The above method produces the value as expected. However, now I want to get the value in the reverse order (big endian format), but it gives me some crazy value of 14471936:</p>
  
  <pre><code>private long bigEndianReadBytes(List&lt;Integer&gt; packet, int idx, int size){
      long val=0;
      int element;
  
      for(int i=size;i&gt;0;i--, idx++){
          element = packet.get(idx);
          val |= element &lt;&lt; (8 * i);
      }
  
      return val;
  }
  </code></pre>
  
  <p>What might be wrong with this method?</p>