[prog] [off-topic]Hash key

Sachin sachinr at vsnl.in
Mon Jun 9 20:09:30 EST 2003


Dear ladies and gentlemen,
                                                    Please look at the code
snippet given below.
int hash (key)
datum key;
{
  unsigned int value; /* Used to compute the hash value.  */
  int   index;  /* Used to cycle through random values. */

  /* Set the initial value from key. */
  value = 0x238F13AF * key.dsize;
/* 0x238F13AF: in Decimal->596579247 and in    Binary ->
100011100011110001001110101111 */

  for (index = 0; index < key.dsize; index++)
    value = (value + (key.dptr[index] << (index*5 % 24))) & 0x7FFFFFFF;

  value = (1103515243 * value + 12345) & 0x7FFFFFFF;

  /* Return the value. */
  return((int) value);
}

                                                        This code is the one
which converts a key(a character string) into a 31 bit hash key.My question
is:Can anyone shed light at what this code does?.What is the principle
involved?Why are these values chosen?


Sachin

Ps:This is a part of the code from GDBM library.



More information about the Programming mailing list