Java Integer Hash intHash(int input)

Here you can find the source of intHash(int input)

Description

Redistribute integer identifier keyspace using Knuth's multiplicative method.

License

Open Source License

Declaration

public static int intHash(int input) 

Method Source Code

//package com.java2s;

public class Main {
    /** Redistribute integer identifier keyspace using Knuth's multiplicative method.
     *  Modified to use long internally since java does not have unsigned int.
     *  This performs one-to-one remapping from integer to integer to avoid collisions
     *  https://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key
     *//*from   www . j  a  va 2 s .c  om*/
    public static int intHash(int input) {
        // convert to unsigned long
        long unsignedValue = ((long) input) - Integer.MIN_VALUE;
        long unsignedIntMax = (1l << 32);
        // Knuth's multiplicative method
        long unsignedHashValue = ((unsignedValue * 2654435761l) % unsignedIntMax);
        // convert back to signed integer
        return (int) (unsignedHashValue + Integer.MIN_VALUE);
    }
}

Related

  1. intHash(final long a)
  2. intHash(int i)
  3. intHash(int key)
  4. intHash(int number, int max)
  5. intHash(long a)
  6. intHashCode(final int value)