# i dont understand what is 0x7fffffff mean. is there any other way to code getHashValue method?

``````public int getHashValue(K key){
return (key.hashCode() & 0x7fffffff) % size;
}
``````

i dont understand what is 0x7fffffff mean. is there any other way to code getHasValue method?

• It's a hex constant. This is rather basic. Why do you think you need another way? – user207421 Apr 1 '18 at 2:13

## 3 Answers

The constant `0x7FFFFFFF` is a 32-bit integer in hexadecimal with all but the highest bit set.

Despite the name, this method isn't getting the hashCode, rather looking for which bucket the key should appear in for a hash set or map.

When you use `%` on negative value, you get a negative value. There are no negative buckets so to avoid this you can remove the sign bit (the highest bit) and one way of doing this is to use a mask e.g. `x & 0x7FFFFFFF` which keeps all the bits except the top one. Another way to do this is to shift the output `x >>> 1` however this is slower.

A slightly better approach is to use "take the modulus and apply Math.abs". This uses all the bits of the hashCode which might be better.

e.g.

``````public int getBucket(K key) {
return Math.abs(key.hashCode() % size);
}
``````

Even this is not ideal as some hashCode() have a poor distribution resulting in a higher collision rate. You might want to agitate the hashcode before the modulus etc.

``````public int getBucket(K key) {
return Math.abs(hash(key) % size);
}
``````

HashMap in java 8 uses this

``````static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
``````

The function is simple as it handles collisions efficiently. In Java 7 it used this function.

``````static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
``````

That's the Hexadecimal representation of the Max. Integer

You can check here

`0x7fffffff` just remove the signal getting the complement of the number.

Using Java REPL you can see the results of the operation

``````> -7 & 0x7fffffff
java.lang.Integer res1 = 2147483641
> 2147483641 & 0x7fffffff
java.lang.Integer res2 = 2147483641
> 2147483641 + 6
java.lang.Integer res3 = 2147483647
> 7 + 2147483641
java.lang.Integer res4 = -2147483648
``````

In the binary representation, the first bit is the signal. If you set it to zero you will get positive complementary if the number is negative. Or the same number if positive.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7223919,"math_prob":0.91103464,"size":2543,"snap":"2021-04-2021-17","text_gpt3_token_len":662,"char_repetition_ratio":0.113036625,"word_repetition_ratio":0.11235955,"special_character_ratio":0.2898152,"punctuation_ratio":0.11728395,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9940924,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-21T09:15:25Z\",\"WARC-Record-ID\":\"<urn:uuid:329dd20e-4ffa-465d-bf9f-ccc299b4fccd>\",\"Content-Length\":\"182850\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c66810d5-9c06-4085-aab4-3c820e67c8be>\",\"WARC-Concurrent-To\":\"<urn:uuid:a5f71585-b229-4d59-b8cb-48d44052b2ef>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/49592995/i-dont-understand-what-is-0x7fffffff-mean-is-there-any-other-way-to-code-gethas\",\"WARC-Payload-Digest\":\"sha1:KAG7V4UD2CR6OT3GHPX2ZZZ4YEH74RPP\",\"WARC-Block-Digest\":\"sha1:Y6O6QY77TFNW2SCTJAL2XJGRLG7Z2DRG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618039526421.82_warc_CC-MAIN-20210421065303-20210421095303-00024.warc.gz\"}"}