title: Writing Bits across two bytes
body: <p>I have a requirement where I have to write a HEX value to an 11 bit "container" across two bytes. The layout is as follows: Byte 5(bit 3) is the lsb and data goes to Byte 4(bit 5)which is the msb. What is the best method for writing data to the bit locations in question. </p>
  
  <p>For example if I need to write the value 0x1DA to these locations what would be the best way of doing that while keeping the bit ordering correct.</p>
  
  <p>FYI the is for CAN communication for an 8 byte CAN message.  </p>
  
  <p>I was thinking of doing this:</p>
  
  <pre><code>_templsb = DatatoWrite &amp; 0x1F;
  _templsb &lt;&lt;= 3;
  Byte5 &amp;= ~0xF8;
  Byte5 = Byte5 | _templsb;
  
  _tempmsb = DatatoWrite &amp; 0x7E0;
  _tempmsb &gt;&gt;= 5;
  Byte4 &amp;= ~0x3F;
  Byte4 =  Byte4 | _tempmsb;
  </code></pre>
  
  <p>Is something like this the best way to do it?</p>