这从一开始就是错误的
String lastdigits="1000110";
int lastdigitsint=Integer.parseInt(lastdigits);
最后一个数字值将是 1000110 -那是一千万百零十
,所以二进制表示与您的预期大不相同。你想做的很简单:
int lastdigitsint=0b1000110;
这是正确的二进制整数。
所以:
public static void main(String[] args) {
String lastdigits = "1000110";
int overflow = 0b11;
int lastdigitsint = Integer.parseInt(lastdigits);
int result = lastdigitsint ^ overflow;
System.out.println("Dec:" + lastdigitsint);
System.out.println("Dec:" + overflow);
System.out.println("Dec:" + result);
System.out.println("Bin" + Integer.toBinaryString(lastdigitsint));
System.out.println("Bin" + Integer.toBinaryString(overflow));
System.out.println("Bin" + Integer.toBinaryString(result));
System.out.println("And now the proper way:");
lastdigitsint = 0b1000110;
result = lastdigitsint ^ overflow;
System.out.println("Dec:" + lastdigitsint);
System.out.println("Dec:" + overflow);
System.out.println("Dec:" + result);
System.out.println("Bin" + Integer.toBinaryString(lastdigitsint));
System.out.println("Bin" + Integer.toBinaryString(overflow));
System.out.println("Bin" + Integer.toBinaryString(result));
}
结果:
Dec:1000110
Dec:3
Dec:1000109
Bin:11110100001010101110
Bin:11
Bin:11110100001010101101
And now the proper way:
Dec:70
Dec:3
Dec:69
Bin:1000110
Bin:11
Bin:1000101
接缝对我来说是合法的。