title: represent an integer using binary in java language
body: <p>Here is the problem:</p>
  
  <p>You're given 2 32-bit numbers, N &amp; M, and two bit positions, i &amp; j. write a method to set all bits between i and j in N equal to M (e.g. M becomes a substring of N at locating at i
  and starting at j)</p>
  
  <p>For example:
    input: 
    int N = 10000000000, M = 10101, i = 2, j = 6;
    output:
      int N = 10001010100</p>
  
  <p>My solution:</p>
  
  <pre><code>step 1: compose one mask to clear sets from i to j in N 
   mask=   ( ( ( ((1&lt;&lt;(31-j))-1) &lt;&lt; (j-i+1) ) + 1 ) &lt;&lt; i  ) - 1 
   for the example, we have 
         mask= 11...10000011
  step 2: 
        (N &amp; mask) | (M&lt;&lt;i)
  </code></pre>
  
  <p>Question:
      what is the convenient data type to implement the algorithm? for example
     we have int n = 0x100000 in C, so that we can apply bitwise operators on n.
     in Java, we have BitSet class, it has clear, set method, but doesnt support 
     left/right shift operator; if we use int, it supports left/right shift, but 
     doesnt have binary representation (I am not talking about binary string representation)
     what is the best way to implement this? </p>
  
  <p>Code in java (after reading all comments):</p>
  
  <pre><code>int x = Integer.parseInt("10000000000",2);
  int x = Integer.parseInt("10101",2);
  int i = 2, j = 6;
  public static int F(int x, int y, int i, int j){
  int mask = (-1&lt;&lt;(j+1)) | (-1&gt;&gt;&gt;(32-i));
  return (mask &amp; x ) | (y&lt;&lt;i);   
  }        
  </code></pre>