JAVA程序
public class Abs{ public static void main(String[] args){
byte a=(byte)139;
byte b=(byte)-129;
System.out.println("a="+a);
System.out.println("b="+b);
}
}结果:a=-128
b=127
谁可以解释一下?
public class Abs{ public static void main(String[] args){
byte a=(byte)139;
byte b=(byte)-129;
System.out.println("a="+a);
System.out.println("b="+b);
}
}结果:a=-128
b=127
谁可以解释一下?
byte内存分配8个位。其中首位为符号位。负数是以补码形式来表示的
0 1 1 1 1 1 1 1 127+12(1100)=139
+0 0 0 0 1 1 0 0
1 0 0 0 1 0 1 1 这个正是-117内存中补码的表示
1 0 0 0 0 0 0 0 -128内存补码表示
+1 1 1 1 1 1 1 1 -128-1=-129 -1内存补码为1 1 1 1 1 1 1 1
(溢出) 0 0 1 1 1 1 1 1 1 这个是127