title: java transfer bits at offset and length from byte to int
body: <p>I am trying to transfer a certain number of bits in a byte to the beginning of an int but its not working out as planned.</p>
  
  <pre><code>public int transfer(byte b, int offset, int len, int dest, int bitsInUSe){
               byte mask  = (byte) ((byte)  ((1 &lt;&lt; len) - 1) &lt;&lt; offset);
                 dest = dest&lt;&lt; bitsInUSe;
            dest = b &amp; mask;
                return dest ;
  }
  </code></pre>
  
  <p>eg, with offset 2 and len 3 of byte 00111000 should produce the int>
  00000000000000000000000000000110</p>
  
  <p>I only need to put the bits at the beginning of the int but I would need to move any bits that have previously been assigned to the left so they are not overridden, hence the bitsInUse variable.</p>