问题描述
我在Ubuntu 12.04上使用Oracle Java 7.51,并试图这样做
I am using Oracle Java 7.51 on Ubuntu 12.04, and trying to do this
long a = 0x0000000080000001 ^ 0x4065DE839A6F89EEL;
System.out.println("result "+ Long.toHexString(a));
Output: result bf9a217c1a6f89ef
但我期待结果 4065de831a6f89ef
,因为^运算符是Java中的按位异或。我读错了Java规范的哪一部分?
But I was expecting result to be 4065de831a6f89ef
, since ^ operator is a bitwise XOR in Java. Which part of Java specification am I reading wrong?
推荐答案
在第一个整数文字的末尾需要 L
:
You need an L
at the end of the first integer literal:
long a = 0x0000000080000001L ^ 0x4065DE839A6F89EEL;
否则它是 int
字面值,而不是a long
(忽略前导零)。然后 ^
运算符将第一个操作数值从0x80000001提升为 long
,但由于符号位已设置,促销结果为0xFFFFFFFF80000001L。
Otherwise it is an int
literal, not a long
(the leading zeroes being ignored). The ^
operator then promotes the first operand value from 0x80000001 to a long
, but since the sign bit is set, the result of the promotion is 0xFFFFFFFF80000001L.
这篇关于按位XOR java长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!