title: Swap nibbles in a byte
body: <p>Consider the code below which swaps nibbles in a byte:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  unsigned char swapNibbles(unsigned char x)
  {
      return ( (x &amp; 0x0F)&lt;&lt;4 | (x &amp; 0xF0)&gt;&gt;4 );
  }
  
  int main()
  {
      unsigned char x = 100;
      printf("%u", swapNibbles(x));
      return 0;
  }
  </code></pre>
  
  <p>Why is anding with 0x0F and 0xF0 necessary? Instead I can just write </p>
  
  <pre><code>return ((x&lt;&lt;4) | (x&gt;&gt;4));
  </code></pre>
  
  <p>I have googled about it and people seem to say that it won't work with negative numbers as it would pad the number with ones in right shift however anding with 0xF0 would not make the number positive?
  What am I missing?</p>