I noticed that the hash function code as part of java.util.Hashtable#get(K key) does the following: int index = (hash & 0x7FFFFFFF) % tab.length;. Is this binary ‘and’ operation meant only to reset the sign bit? and therefore avoid negative table access.
UPDATE: the fact that they ‘and’ with a 0x7FFFFFFF and not a 0xEFFFFFFF puzzled me. Why does the sign requires a full byte rather than a single bit?
Yes, that’s correct. This is to avoid negative indexing into the underlying array in the hash table.
Note that in languages with unsigned integer types like C or C++ this could be avoided by just using unsigned values in the hash function.
EDIT: Given your new question about why
0x7FFFFFFversus0xEFFFFFF– the first of these numbers is all 1s with the top bit set to 0. The second of these does not have this property; it comes out to1110followed by lots of 1s. Therefore, masking with the first will clear the 1 bit, while masking with the second might not do this.Hope this helps!