Bitwise XOR Java length

I use Oracle Java 7.51 on Ubuntu 12.04 and try to do so

long a = 0x0000000080000001 ^ 0x4065DE839A6F89EEL;
System.out.println("result "+ Long.toHexString(a));

Output: result bf9a217c1a6f89ef

But I expect the result to be 4065de831a6f89ef, because the ^ operator is a bitwise XOR in Java What part of the Java specification did I read wrong?

Solution

An L is required at the end of the first integer text:

long a = 0x0000000080000001L ^ 0x4065DE839A6F89EEL;

Otherwise, it is an int literal, not a long (ignored leading zero) Then, ^ operator promotes the value of the first operation from 0x80000001 to long, but since the sign bit is set, the promotion result is 0xffffff8000001l

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>