title: Bit shift and pointer oddities in C, looking for explanations
body: <p>I discovered something odd that I can't explain. If someone here can see what or why this is happening I'd like to know. What I'm doing is taking an unsigned short containing 12 bits aligned high like this:</p>
  
  <p><code>1111 1111 1111 0000</code></p>
  
  <p>I then want to shif the bits so that each byte in the short hold 7bits with the MSB as a pad. The result on what's presented above should look like this:</p>
  
  <p><code>0111 1111 0111 1100</code></p>
  
  <p>What I have done is this:</p>
  
  <pre><code>unsigned short buf = 0xfff;
  //align high
  buf &lt;&lt;= 4;
  
  buf &gt;&gt;= 1;
  *((char*)&amp;buf) &gt;&gt;= 1;
  </code></pre>
  
  <p>This gives me something like looks like it's correct but the result of the last shift leaves the bit set like this:</p>
  
  <p><code>0111 1111 1111 1100</code> </p>
  
  <p>Very odd. If I use an unsigned char as a temporary storage and shift that then it works, like this:</p>
  
  <pre><code>unsigned short buf = 0xfff;
  buf &lt;&lt;= 4;
  
  buf &gt;&gt;= 1;
  tmp = *((char*)&amp;buf);
  *((char*)&amp;buf) = tmp &gt;&gt; 1;
  </code></pre>
  
  <p>The result of this is:</p>
  
  <p><code>0111 1111 0111 1100</code></p>
  
  <p>Any ideas what is going on here? </p>