title: Sum of two numbers with bitwise operator
body: <p>I am pasting the code to find the sum of two numbers with bitwise operator. Please suggest if it can be optimized. Thanks...</p>
  
  <pre><code>public static int getSum(int p, int q)
  {
  int carry=0, result =0;
  for(int i=0; i&lt;32; i++)
  {
      int n1 = (p &amp; (1&lt;&lt;(i)))&gt;&gt;(i); //find the nth bit of p
      int n2 = (q &amp; (1&lt;&lt;(i)))&gt;&gt;(i); //find the nth bit of q
  
      int s = n1 ^ n2 ^ carry; //sum of bits
      carry = (carry==0) ? (n1&amp;n2): (n1 | n2); //calculate the carry for next step
      result = result | (s&lt;&lt;(i)); //calculate resultant bit
  }
  
  return result;
  }
  </code></pre>