Domanda

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.

È stato utile?

Soluzione 2

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.

Altri suggerimenti

Append an L here

long delta = 0x9e3779b9L; // note final L

so that it becomes an integer literal of type long. Without the L (or l), the int value of the literal 0x9e3779b9 is taken which overflows into negative value and then that value is widened to a long.

Use the unsigned right shift operator >>> instead of the normal right shift operator >>. Thus the sign bit will not be preserved but instead be shifted as well. This lets you treat the Java integer types as unsigned integers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top