From charlesreid1

Revision as of 11:05, 25 June 2017 by Admin (talk | contribs)

Cyclic permutation is a method for generating hash functions from data.

The basic idea is to generate a binary representation of the data (typically stored in a 32-bit integer, so wrapped into 32 bits) with a series of 32 0s and 1s, then to cyclically shift the digits of this binary number. If the data are the same, they should result in the same binary number, which will be shifted into the same resulting number when the hash code is computed. Collisions will happen if two objects end up having the same binary representation.

For example, to cyclically shift the digits of 10001111 2 digits to the right, the number would become 111000011.

A simple implementation of the cyclic permutation hash code calculation in Java is given as a static method below:

	public static int hashCode(String s) { 
		int shift = 5;
		int h = 0;
		for(int i=0; i<s.length(); i++) { 
			h = (h<<(shift)) | (h>>>(32-shift));
			h += (int)(s.charAt(i));
		}
		return h;
	}