title: Change sign using bitwise operators
body: <p>How to change the sign of int using bitwise operators? Obviously we can use <code>x*=-1</code> or <code>x/=-1</code>. Is there any fastest way of doing this? </p>
  
  <p>I did a small test as below. Just for curiosity...</p>
  
  <pre><code>public class ChangeSign {
      public static void main(String[] args) {
          int x = 198347;
          int LOOP = 1000000;
          int y;
          long start = System.nanoTime();
          for (int i = 0; i &lt; LOOP; i++) {
              y = (~x) + 1;
          }
          long mid1 = System.nanoTime();
          for (int i = 0; i &lt; LOOP; i++) {
              y = -x;
          }
          long mid2 = System.nanoTime();
          for (int i = 0; i &lt; LOOP; i++) {
              y = x * -1;
          }
          long mid3 = System.nanoTime();
          for (int i = 0; i &lt; LOOP; i++) {
              y = x / -1;
          }
          long end = System.nanoTime();
          System.out.println(mid1 - start);
          System.out.println(mid2 - mid1);
          System.out.println(mid3 - mid2);
          System.out.println(end - mid3);
      }
  }
  </code></pre>
  
  <p>Output is almost similar to :</p>
  
  <pre><code>2200211
  835772
  1255797
  4651923
  </code></pre>