java 整型int占4个字节32位,两个数异或后移动31位判断结果,如果是1则异号,如果是0则同号
public class ShowEnviromentViarible { public static void main(String[] args) {
int num1 = 1;
int num2 = -1;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println((num1 ^ num2) >>> 31); //无符号移动31位,最左边补0
num1 = 1;
num2 = 1;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println((num1 ^ num2) >>> 31);
num1 = -1;
num2 = -1;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println((num1 ^ num2) >>> 31);
} }
output:
num1 = 1
num2 = -1
1
num1 = 1
num2 = 1
0
num1 = -1
num2 = -1
0