title: Inserting bits into byte
body: <p>I was looking at an example of reading bits from a byte and the implementation looked simple and easy to understand.  I was wondering if anyone has a similar example of how to insert bits into a byte or byte array, that is easier to understand and also implement like the example below.</p>
  
  <p>Here is the <a href="http://bytes.com/topic/c-sharp/answers/505085-reading-bits-byte-file" rel="nofollow noreferrer">example I found</a> of reading bits from a byte:</p>
  
  <pre><code>    static int GetBits3(byte b, int offset, int count)
      {
             return (b &gt;&gt; offset) &amp; ((1 &lt;&lt; count) - 1);
      }
  </code></pre>
  
  <p>Here is what I'm trying to do. This is my current implementation, I'm just a little confused with the bit-masking/shifting, etc., so I'm trying to find out if there is an easier way to do what I'm doing</p>
  
  <pre><code>BYTE Msg[2];
  Msg_Id = 3;
  Msg_Event = 1;
  Msg_Ready = 2;
  
  Msg[0] = ( ( Msg_Event &lt;&lt; 4 ) &amp; 0xF0 ) | ( Msg_Id &amp; 0x0F ) ;
  Msg[1] = Msg_Ready  &amp; 0x0F;     //MsgReady &amp; Unused
  </code></pre>