Luc Taylor
Luc Taylor

Reputation: 39

JEXL2.1 Circular Bit Shift To Mirror Java

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.

Upvotes: 0

Views: 40

Answers (1)

henrib
henrib

Reputation: 364

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);
    }
}

Upvotes: 1

Related Questions