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

  • SEARCH
  • Home
  • 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 7885047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:57:38+00:00 2026-06-03T04:57:38+00:00

IMPORTANT NOTICE: This is not a discussion thread for people to give me their

  • 0

IMPORTANT NOTICE:

This is not a discussion thread for people to give me their opinion about hashing. I just need to know how to make the given function work in java — an example would be best.

PROBLEM:

Trying to hone my understanding of hash functions for a pending interview, I watch two free lectures by MIT computer science professors (http://videolectures.net/mit6046jf05_leiserson_lec08/). So after the lecture, I am trying to implement the following hash function in java.

h(k) = (A·k mod 2^w) >> (w – r)
WHERE
r: m, the size of the array, is a power of 2 such that m=2^r
w: the computer has w-bit words, such as 32-bit or 64-bit computer
k: the value I am to find a key for
A: a random odd number (prime would be great) between 2^(w-1) and 2^w    

I thought this would be easy to implement in java. But when I do 2^w where w=32, I get inaccurate results in Java. In real life 2^32 = 4294967296 but not in java, which truncates the result to 2^31 - 1 or 2147483647.

Does anyone know how to fix this problem so to implement the function in Java?

EDIT:

I see a lot of the replies focus on 32. What if my computer is 64 bit? I am stuck with setting w = 32 because I am using Java?

  • 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-06-03T04:57:39+00:00Added an answer on June 3, 2026 at 4:57 am

    Some of the terms are redundant because Java assumes this behaviour anyway.

    A·k mod 2^w
    

    In Java, integer multiplication overflows and thus does a mod 2^w (with a sign). The fact that it has a sign doesn’t matter if you are then shifting by at least one bit.

    Shift of (w - r) is the same as a shift of -r in Java (the w is implied by the type)

    private static final int K_PRIME = (int) 2999999929L;
    
    public static int hash(int a, int r) {
       // return (a * K_PRIME % (2^32)) >>> (32 - r);
       return (a * K_PRIME) >>> -r;
    }
    

    for 64-bit

    private static final long K_PRIME = new BigInteger("9876534021204356789").longValue();
    
    public static long hash(long a, int r) {
        // return (a * K_PRIME % (2^64)) >>> (64 - r);
        return (a * K_PRIME) >>> -r;
    }
    

    I have written this example to show you can do the same thing in BigInteger and why you wouldn’t. 😉

    public static final BigInteger BI_K_PRIME = new BigInteger("9876534021204356789");
    private static long K_PRIME = BI_K_PRIME.longValue();
    
    public static long hash(long a, int r) {
        // return (a * K_PRIME % (2^64)) >>> (64 - r);
        return (a * K_PRIME) >>> -r;
    }
    
    public static long biHash(long a, int r) {
        return BigInteger.valueOf(a).multiply(BI_K_PRIME).mod(BigInteger.valueOf(2).pow(64)).shiftRight(64 - r).longValue();
    }
    
    public static void main(String... args) {
        Random rand = new Random();
        for (int i = 0; i < 10000; i++) {
            long a = rand.nextLong();
            for (int r = 1; r < 64; r++) {
                long h1 = hash(a, r);
                long h2 = biHash(a, r);
                if (h1 != h2)
                    throw new AssertionError("Expected " + h2 + " but got " + h1);
            }
        }
    
        int runs = 1000000;
        long start1 = System.nanoTime();
        for (int i = 0; i < runs; i++)
            hash(i, i & 63);
        long time1 = System.nanoTime() - start1;
    
        long start2 = System.nanoTime();
        for (int i = 0; i < runs; i++)
            biHash(i, i & 63);
        long time2 = System.nanoTime() - start2;
        System.out.printf("hash with long took an average of %,d ns, " +
                "hash with BigInteger took an average of %,d ns%n",
                time1 / runs, time2 / runs);
    }
    

    prints

    hash with long took an average of 3 ns, \
        hash with BigInteger took an average of 905 ns
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Screenshot Important Note : This application does not support Internet Explorer. I will be
I know this is right in front of me, but I need to add
I noticed an odd (to me) mod_rewrite thing happening. Fixing it is not important
Important : Please see this very much related question: Return multiple values in C++
Is it really important to know algorithms to build mobile applications? I have strong
i am following this tutorial: http://tutorial.symblog.co.uk/docs/validators-and-forms.html at the swift mailer part i am not
My intention in this question is not to be pedantic, but rather to explore
This is a very very important sql query after which my whole website is
(edit) more info. First notice new virtual. This class inherits a base class which
I think this is an !important issue. I have a dialog with a set

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.