Question

I am doing a pokerHand evaluator in Java, for what I m using one implemented in C (http://www.suffecool.net/poker/evaluator.html). I have "translated" the code and it works pretty well and fast. Now I want to add some HashTables to make it faster, and it looks pretty simple, as it is explained here: http://www.paulsenzee.com/2006/06/some-perfect-hash.html But it ends up that the code doesn't work, and I ve realized that is in the following function:

unsigned find_fast(unsigned u)
{
    unsigned a, b, r;
    u += 0xe91aaa35;
    u ^= u >> 16;
    u += u << 8;
    u ^= u >> 4;
    b  = (u >> 8) & 0x1ff;
    a  = (u + (u << 2)) >> 19;
    r  = a ^ hash_adjust[b];
    return r;
}

I think "unsigned" without anything else in C is an integer, but in java this function doesnt generate the same as in C, and r sometimes is a negative value, which does not make sense. As far as I know there is nothing like the unsigned operator in Java, and I think is causes problem understanding some representations as negative numbers. Could someone help me to translate that code into Java? Thanks a lot!

Was it helpful?

Solution

try changing unsigned to int and >> shift operator to java's >>> unsigned shift operator.

this should be valid if your unsigned type is 32 bits long.

in java all number primitives are signed, but using the hint, your method should work similar to C version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top