Search code examples
javahexsign

Problems in Java Hex Numbers


I will edit the question.

I shouldn't possibly get a negative number in L and R. but the final answer is negative for both of them.

here

    long sum = 0L;
    long delta = 0x9e3779b9L;
    long L=0x01234567;
    long R=0x89ABCDEF;
    long K0 = 0xA56BABCDL;
    long K1 = 0x00000000L;
    long K2 = 0xFFFFFFFFL;
    long K3 = 0xABCDEF01L;

for (int i = 0; i < 32; i++) {
    sum = sum + delta;
    L = L + (((R << 4) + K0) ^ (R + sum) ^ ((R >> 5) + K1));
    R = R + (((L << 4) + K2) ^ (L + sum) ^ ((L >> 5) + K3));
    System.out.println(L + "   " + R);
}

Is there any way to use Hex data type in java and make my life easy???? Is there any Hex API which supports the shift operations and the XOR operations easily. This shouldn't be this hard.


Solution

  • If you need to avoid overflow by shifting left, you want to use a BigInteger instead of a long to store your number. BigInteger has methods to support or, as well as left and right shift operations, and since BigInteger has arbitrary precision, left shift won't overflow or switch the sign of the number.