0

I wrote a hashing function that takes a 64 bit integer in, and outputs a 64 bit integer out in JAVA and translated it to JEXL 2.1

I need it to return the same results in both, but am not too picky about how it works

Everything was fine until this line

state = (state << 7) | (state >>> (64 - 7)); // couldn't get bit shift to work in JEXL

I cannot replicate this behavior in in JEXL2.1 Bit shift doesn't seem to exist at all, I tried to replicate with multiplication and division and was getting odd off by 1 errors.

By the way.. I'm open to changing the java as long as it basically does the same thing.

Luc Taylor
  • 29
  • 5
  • Probability that the last ten bits flip from changing the last bit is with the rotation is much worse without the rotation – Luc Taylor Jul 19 '23 at 21:41
  • Gave up. Changed it. ``` boolean wasNegative = (state < 0); state &= 9223372036854775807L; long last7 = state & 127; state >>= 7; state += last7 << 56; if(wasNegative) { state = -state; } ``` and this in JEXL ``` if(state<0) { state = state & 9223372036854775807L; last7 = state & 127; state = state / 128; state = state + last7 * 72057594037927936L; state = -state; } else { state = state & 9223372036854775807L; last7 = state & 127; state = state / 128; state = state + last7 * 72057594037927936L; } return state + 0; ``` – Luc Taylor Jul 20 '23 at 16:57

1 Answers1

1

I'd recommend using JEXL 3.3 instead which does implement the bitshift operators. The following example works in 3.3:

long hashState(long state) {
    return (state << 7) | (state >>> (64 - 7));
}
@Test
public void testSOHash() {
    String src ="state ->  (state << 7) | (state >>> (64 - 7))";
    final JexlEngine jexl = new JexlBuilder().create();
    JexlScript script = jexl.createScript(src);
    Object result;
    long[] checks = new long[]{33, 89, 345678990, -23233, -169, 0};
    for(long arg : checks) {
        result = script.execute(null, arg);
        Assert.assertTrue(result instanceof Number);
        long nresult = ((Number) result).longValue();
        Assert.assertEquals("checking " + arg, hashState(arg), nresult);
    }
}
henrib
  • 187
  • 6