title: how to replace given nibbles with another set of nibbles in an integer
body: <p>Suppose you have an integer <code>a = 0x12345678</code> &amp; a short <code>b = 0xabcd</code></p>
  
  <p>What i wanna do is replace the given nibbles in <code>integer a</code> with nibbles from <code>short b</code></p>
  
  <p><strong>Eg:</strong> Replace 0,2,5,7th nibbles in <code>a = 0x12345678</code> (where 8 = 0th nibble, 7=1st nibble, 6=2nd nibble and so on...) with nibbles from <code>b = 0xabcd</code> (where d = 0th nibble, c=1st nibble, b=2nd nibble &amp; so on...)</p>
  
  <p><strong>My approach</strong> is -</p>
  
  <ol>
  <li>Clear the bits we're going to replace from <code>a</code>.
  like <code>a = 0x02045070</code></li>
  <li>Create the mask from the short <code>b</code> like <code>mask = 0xa0b00c0d</code></li>
  <li>bitwise <code>OR</code> them to get the result. <code>result = a| mask</code> i.e <code>result = 0xa2b45c7d</code> hence nibbles replaced.</li>
  </ol>
  
  <p><strong>My problem</strong> is I don't know any <strong>efficient</strong> way to create the desired mask (like in step 2) from the given <code>short b</code></p>
  
  <p>If you can give me an <strong>efficient</strong> way of doing so, it would be a great help to me and I thank you for that in advance ;)</p>
  
  <p>Please ask if more info needed.</p>
  
  <p><strong>EDIT:</strong> <br>
  My code to solve the problem (not good enough though)<br>
  Any improvement is highly appreciated.<br></p>
  
  <pre><code>int index[4] = {0,1,5,7}; // Given nibbles to be replaced in integer
  int s = 0x01024300; // integer mask i.e. cleared nibbles
      int r = 0x0000abcd; // short (converted to int )
       r = ((r &amp; 0x0000000f) &lt;&lt; 4*(index[0]-0)) | 
           ((r &amp; 0x000000f0) &lt;&lt; 4*(index[1]-1)) | 
           ((r &amp; 0x00000f00) &lt;&lt; 4*(index[2]-2)) |
           ((r &amp; 0x0000f000) &lt;&lt; 4*(index[3]-3));
      s = s|r;
  </code></pre>