【发布时间】:2018-04-25 09:38:09
【问题描述】:
我想知道 java 如何发现按位运算的结果是 -ve 还是 +ve?
int x=-5;
int y=8;
System.out.println(x&y); //8
System.out.println(x|y); //-5
x->1 0 1 1 (2's complement)
y->1 0 0 0
x & y -> 1 0 0 0 ->8
x | y -> 1 0 1 1 ->-5(2's complement)
java如何知道1 0 1 1是-5?
为什么不直接给 o/p 作为 1 0 1 1 的十进制等值 11 呢?
它是否对每个结果应用 2 的补码?
我看过汇编代码。是 IAND 和 IOR 指令。
【问题讨论】:
-
他没有解释任何与 -ve 数字相关的事情..
-
@AnandChoudhary 链接:
7) Integral types in Java (int, long, short and byte) are signed numbers in Java where most significant bit (MSB) represent sign of number. 1 will denote a negative number and 0 as MSB will denote a positive numbers -
-5 的二进制表示是
11111111111111111111111111111011而不是1011。
标签: java bitwise-operators bitwise-and bitwise-or