Setting a nibble of a 32-bit integer to a certain value
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/14427737/setting-the-nibble-for-an-int-java">Setting the nibble for an int - java</a>  </p>
</blockquote>



<p>I am stuck on how to replace a 4-bit value to a certain position of an original 32-bit integer. All help is greatly appreciated!</p>

<pre><code>/**
 * Set a 4-bit nibble in an int.
 * 
 * Ints are made of eight bytes, numbered like so:
 *   7777 6666 5555 4444 3333 2222 1111 0000
 *
 * For a graphical representation of this:
 *   1 1 1 1 1 1                 
 *   5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |Nibble3|Nibble2|Nibble1|Nibble0|
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * 
 * Examples:
 *     setNibble(0xAAA5, 0x1, 0); // =&gt; 0xAAA1
 *     setNibble(0x56B2, 0xF, 3); // =&gt; 0xF6B2
 * 
 * @param num The int that will be modified.
 * @param nibble The nibble to insert into the integer.
 * @param which Selects which nibble to modify - 0 for least-significant nibble.
 *            
 * @return The modified int.
 */
public static int setNibble(int num, int nibble, int which)
{
           int shifted = (nibble &lt;&lt; (which&lt;&lt;2));
       int temp = (num &amp; shifted);
           //this is the part I am stuck on, how can  I replace the original
           // which location with the nibble that I want? Thank you!
       return temp;
}
</code></pre>
