Change sign using bitwise operators
问题 How to change the sign of int using bitwise operators? Obviously we can use x*=-1 or x/=-1 . Is there any fastest way of doing this? I did a small test as below. Just for curiosity... 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 < LOOP; i++) { y = (~x) + 1; } long mid1 = System.nanoTime(); for (int i = 0; i < LOOP; i++) { y = -x; } long mid2 = System.nanoTime(); for (int i = 0; i