# Paul Hsieh's Hash Function. : Hash Code « Development Class « Java

Paul Hsieh's Hash Function.

```
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

//package org.ancora.SharedLibrary;

/**
* Methods for bit manipulation.
*
* @author Joao Bispo
*/
public class Util{
private static final long MASK_16_BITS = 0xFFFFL;
private static final int MASK_BIT_1 = 0x1;
/**
* Paul Hsieh's Hash Function.
*
* @param data data to hash
* @param dataLength length of the data, in bytes
* @param hashedValue previous value of the hash. If it is the start of the
* method, used the length of the data (ex.: 8 bytes).
* @return
*/
public static int superFastHash(long data, int hash) {
int tmp;
//int rem;

//if (len <= 0) {
//   return 0;
//}

//rem = len & 3;
//len >>= 2;

//Main Loop
for (int i = 0; i < 4; i += 2) {
// Get lower 16 bits
hash += get16BitsAligned(data, i);
// Calculate some random value with second-lower 16 bits
tmp = (get16BitsAligned(data, i + 1) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
// At this point, it would advance the data, but since it is restricted
// to longs (64-bit values), it is unnecessary).
hash += hash >> 11;
}

// Handle end cases //
// There are no end cases, main loop is done in chuncks of 32 bits.

// Force "avalanching" of final 127 bits //
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;

return hash;
}
/**
* Returns 16 bits from the long number.
*
* @param data
* @param offset one of 0 to 3
* @return
*/
public static int get16BitsAligned(long data, int offset) {
// Normalize offset
offset = offset%4;
//System.out.println("offset:"+offset);
//System.out.println("Data:"+Long.toHexString(data));

// Get the bits
long result = data & mask;

// Put bits in position
return (int) (result >>> (16*offset));
}
}

```

### Related examples in the same category

 1 Computing hash codes 2 A hash-code generator and a collection of static hash-code generation methods. 3 MD5 hash generator 4 Hash 32 String 5 Hash 64 String 6 MD5 hashing: Encodes a string 7 MD5 String 8 Hash Code Builder 9 HashCode generation 10 Get hash code for primitive data types 11 Return as hash code for the given object 12 Null Safe Hash Code 13 A very efficient java hash algorithm, based on the BuzHash algoritm 14 Easy implementation of hashCode 15 An implementation of the HMACT64 keyed hashing algorithm 16 Gets the hash code of an object returning zero when the object is null 17 Unify Hash 18 Secure Hash 19 FNV Hash 20 Jenkins Hash 21 Concurrent Cuckoo hashing using lock striping. Uses R/W locks for resizing. Exercise solution. 22 Concurrent Cuckoo hashing using lock striping. 23 encode Hex 24 Fowler/Noll/Vo hash algorhythm 25 Produces 32-bit hash for hash table lookup. (Jenkins Hash Function) 26 Key Value Hash 27 An extension of WeakReference that implements a sane equals and hashcode method. 28 Dual Key Hash Map 29 A hash map with int key and int values. 30 null Safe Equals and Hash 31 Generates a hash code for a given source code. 32 AtkinsonHash utility class implements the hash algorithm used by HyperCard's ask password command. 33 Hash Code Assist 34 This is a very fast, non-cryptographic hash suitable for general hash-based lookup. 35 An advanced hash table supporting configurable garbage collection semantics of keys and values 36 Hash string