Bit shifting in C++
<p>I have the following struct:</p>

<pre><code>struct SysData
{ 
        // Topic (32 bits)
        UTL_UINT16_Tdef SystemID:11;    // set to decimal 25
        UTL_UINT16_Tdef TypeID:5;       // set to 2 (telemetry type)
        UTL_UINT16_Tdef ContentID;      // set to decimal 1234
}

SysData MsgHdr;
MsgHdr.SystemID   = 25;
MsgHdr.TypeID     = 2;
MsgHdr.ContentID  = 0;
</code></pre>

<p>If I do something like this:</p>

<pre><code>    unsigned int a;
    memcpy(&amp;a, &amp;MsgHdr, sizeof(MsgHdr));
    headerInfo[0] = a &amp; 0x7FF;
    headerInfo[1] = (a &gt;&gt; 16) &amp; 31;
    headerInfo[2] = (a &gt;&gt; 21) &amp; 0xFFFF;
</code></pre>

<p>headerInfo[0] should have the value 25, but it has 36.  What am I doing wrong?</p>
