Bitwise AND in Java Hashtable hash lookups?

2

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?

java
data-structures
hash
hashtable
bit-manipulation
asked on Stack Overflow Jan 20, 2013 by SkyWalker • edited Jan 20, 2013 by SkyWalker

2 Answers

5

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 0x7FFFFFF versus 0xEFFFFFF - 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 to 1110 followed 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!

answered on Stack Overflow Jan 20, 2013 by templatetypedef • edited Jan 20, 2013 by templatetypedef
2

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?

0x7FFFFFFF is just the top bit. In binary it is 01111111111111111111111111111111. Where as 0xEFFFFFFF is 11101111111111111111111111111111 so it would mask out a different bit.

answered on Stack Overflow Jan 20, 2013 by Peter Lawrey

User contributions licensed under CC BY-SA 3.0