I am looking to store user data in a BitSet or EWAHCompressedBitmap. I need to hash to userid to an integer value that determines the location for a particular user in the set. I am wondering if there is a hash function that returns a positive number between 0 and 2147483583. I am trying to use Murmur3 but in Java it returns signed integers. Casting the returned integer to unsigned resulting in a Long that is way to big to be used in the BitSet as an index position.
=> (import '(com.googlecode.javaewah EWAHCompressedBitmap))
com.googlecode.javaewah.EWAHCompressedBitmap
=> (def bm (EWAHCompressedBitmap.))
#'ninegag.core/bm
=> (.set bm 2147483583)
true
=> (.set bm -2147483583)
IndexOutOfBoundsException Position should be between 0 and 2147483583 com.googlecode.javaewah.EWAHCompressedBitmap.set (EWAHCompressedBitmap.java:1230)
My question is: In Java what is the best way to generate a hash value between 0 and 2147483583 or is there any data structure like BitSet that would support a Long as the index position?
Use a bitmask to get the lower 31 bits of the signed integer - that is guaranteed to be an unsigned number. The maximum integer - Integer.MAX_VALUE is a number that has the lower 31 bits set to 1, so it's ideal for use as a bit mask:
int signedHash = ~0; // 32 ones or -1
int unsignedHash = signedHash & Integer.MAX_VALUE;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With