Let's see what the following code outputs:
System.out.println (long.tohexstring (0x100000000l + 0xcafebabe));
A long+ a int,int to a long, and then add to the former. It should be 0x1cafebabe right.
The result output is: Cafebabe
Here is a question: 10 int, the plural is represented by a minus sign. What about the 16 binary int?
Yes, the negative number of the 16 binary is the first 1 in the binary representation for negative numbers.
Binary representation of 0xcafebabe: 11001010111111101011101010111110
So, it actually represents a negative number:-889275714
When the corresponding conversion, int to long, the result is: 1111111111111111111111111111111111001010111111101011101010111110
Added to the corresponding 0x100000000l,
1111111111111111111111111111111111001010111111101011101010111110
+ 100000000000000000000000000000000
-----------------------------------------------------------------------------------
= 11001010111111101011101010111110 (0xcafebabe)
To get the right results, you need to change to:
System.out.println (long.tohexstring (0x100000000l + 0xcafebabeL));
Summarize
When you use 16 to calculate, you need to be aware of negative numbers.
Java Essay: Beware of 16 binary negative numbers