Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6738765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:24:26+00:00 2026-05-26T11:24:26+00:00

Found this code on http://www.docjar.com/html/api/java/util/HashMap.java.html after searching for a HashMap implementation. 264 static int

  • 0

Found this code on http://www.docjar.com/html/api/java/util/HashMap.java.html after searching for a HashMap implementation.

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

Can someone shed some light on this? The comment tells us why this code is here but I would like to understand how this improves a bad hash value and how it guarantees that the positions have bounded number of collisions. What do these magic numbers mean?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:24:27+00:00Added an answer on May 26, 2026 at 11:24 am

    In order for it to make any sense it has to be combined with an understanding of how HashMap allocates things in to buckets. This is the trivial function by which a bucket index is chosen:

    static int indexFor(int h, int length) {
        return h & (length-1);
    }
    

    So you can see, that with a default table size of 16, only the 4 least significant bits of the hash actually matter for allocating buckets! (16 – 1 = 15, which masks the hash by 1111b)

    This could clearly be bad news if your hashCode function returned:

    10101100110101010101111010111111
    
    01111100010111011001111010111111
    
    11000000010100000001111010111111
    //etc etc etc
    

    Such a hash function would not likely be “bad” in any way that is visible to its author. But if you combine it with the way the map allocates buckets, boom, MapFail(tm).

    If you keep in mind that h is a 32 bit number, those are not magic numbers at all. It is systematically xoring the most significant bits of the number rightward into the least significant bits. The purpose is so that “differences” in the number that occur anywhere “across” it when viewed in binary become visible down in the least significant bits.

    Collisions become bounded because the number of different numbers that have the same relevant LSBs is now significantly bounded because any differences that occur anywhere in the binary representation are compressed into the bits that matter for bucket-ing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry just found this code here - http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html and the mutex was explained with
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
I found this via google: http://www.mvps.org/access/api/api0008.htm '******************** Code Start ************************** ' This code was
I have found this project on Codeplex. http://www.codeplex.com/ProjNET I need to integrate this code
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
I found http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/ , but I don't know how to use this code to
I found this code to force using www. RewriteEngine On RewriteCond %{HTTP_HOST} !^www.example.com$ RewriteRule
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html I noticed that in some
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
I'm using the following code to create an xls file from php. http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8 However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.