Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Fixed typo
Source Link
Daniel Fischer
  • 184.5k
  • 19
  • 319
  • 436

I have been failing to understand the design of hash functions. I wa sgoingwas going through an example. As you can see in the function comments, why should you choose 31 as the number to be multiplied. How do you decide? Is that something random or does it mean something?

unsigned int hash(hash_table_t *hashtable, char *str) { unsigned int hashval; /* we start our hash out at 0 */ hashval = 0; /* for each character, we multiply the old hash by 31 and add the current * character. Remember that shifting a number left is equivalent to * multiplying it by 2 raised to the number of places shifted. So we * are in effect multiplying hashval by 32 and then subtracting hashval. * Why do we do this? Because shifting and subtraction are much more * efficient operations than multiplication. */ for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval; /* we then return the hash value mod the hashtable size so that it will * fit into the necessary range */ return hashval % hashtable->size; } 

I have been failing to understand the design of hash functions. I wa sgoing through an example. As you can see in the function comments, why should you choose 31 as the number to be multiplied. How do you decide? Is that something random or does it mean something?

unsigned int hash(hash_table_t *hashtable, char *str) { unsigned int hashval; /* we start our hash out at 0 */ hashval = 0; /* for each character, we multiply the old hash by 31 and add the current * character. Remember that shifting a number left is equivalent to * multiplying it by 2 raised to the number of places shifted. So we * are in effect multiplying hashval by 32 and then subtracting hashval. * Why do we do this? Because shifting and subtraction are much more * efficient operations than multiplication. */ for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval; /* we then return the hash value mod the hashtable size so that it will * fit into the necessary range */ return hashval % hashtable->size; } 

I have been failing to understand the design of hash functions. I was going through an example. As you can see in the function comments, why should you choose 31 as the number to be multiplied. How do you decide? Is that something random or does it mean something?

unsigned int hash(hash_table_t *hashtable, char *str) { unsigned int hashval; /* we start our hash out at 0 */ hashval = 0; /* for each character, we multiply the old hash by 31 and add the current * character. Remember that shifting a number left is equivalent to * multiplying it by 2 raised to the number of places shifted. So we * are in effect multiplying hashval by 32 and then subtracting hashval. * Why do we do this? Because shifting and subtraction are much more * efficient operations than multiplication. */ for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval; /* we then return the hash value mod the hashtable size so that it will * fit into the necessary range */ return hashval % hashtable->size; } 
Source Link
Anusha Pachunuri
  • 1.5k
  • 5
  • 19
  • 39

good hash function

I have been failing to understand the design of hash functions. I wa sgoing through an example. As you can see in the function comments, why should you choose 31 as the number to be multiplied. How do you decide? Is that something random or does it mean something?

unsigned int hash(hash_table_t *hashtable, char *str) { unsigned int hashval; /* we start our hash out at 0 */ hashval = 0; /* for each character, we multiply the old hash by 31 and add the current * character. Remember that shifting a number left is equivalent to * multiplying it by 2 raised to the number of places shifted. So we * are in effect multiplying hashval by 32 and then subtracting hashval. * Why do we do this? Because shifting and subtraction are much more * efficient operations than multiplication. */ for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval; /* we then return the hash value mod the hashtable size so that it will * fit into the necessary range */ return hashval % hashtable->size; }