title: Date encoding through Righ Shift >> and Left Shift << bitwise operation
body: <p>I am trying to solve a C programming problem, which is date encoding in 2 bytes using bit shifting operations and decoding back to date, month and year. While I could also use bitwise <code>&amp;</code> and <code>|</code> operation, I want to use just bit shifting.</p>
  <p>The issue is: even though everything seems to be right the output of date and month has no effect of dual bit shifting operation. The year is okay because there is only one operation. Meaning that the dual operation of bit shift is somehow not behaving as required.</p>
  <p>I have used <code>unsigned char</code> so last sign bit is definitely not the issue. The <code>Showbits</code> function is just to show the bits. I am using gcc g++ compiler with VSCode in Windows 10.</p>
  <pre><code>//  Date encoding in 2-byte number
  
  #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  
  typedef unsigned int uint;
  
  Showbits(int n)
  {
      int i, k, mask;
  
      for (i = 15; i &gt;= 0; i--)
      {
          mask = 1 &lt;&lt; i;
          k = n &amp; mask;
          k == 0 ? printf(&quot;0&quot;) : printf(&quot;1&quot;);
      }
  }
  
  int main()
  {
  
      uint encd_date, date, month, year;
  
      year = 2022;
      month = 9;
      date = 15;
      encd_date = 512 * (year - 1980) + 32 * month + date;
  
      printf(&quot;\nEncoded date:%u\n&quot;, encd_date);
      printf(&quot;\nencd_date: &quot;);
      Showbits(encd_date);
  
      year = (1980 + (encd_date &gt;&gt; 9));
      month = (encd_date &lt;&lt; 7);
      month = (month &gt;&gt; 12);
      date = (encd_date &lt;&lt; 11);
      date = (date &gt;&gt; 11);
  
      printf(&quot;\ndate: &quot;);
      Showbits(date);
  
      printf(&quot;\nmonth: &quot;);
      Showbits(month);
  
      printf(&quot;\nyear: &quot;);
      Showbits(year);
  
      printf(&quot;\nDecoded date %u month %u year %u&quot;, date, month, year);
  
      return 0;
  }
  </code></pre>