Can someone please explain this hashing funtion for strings

This is a discussion on Can someone please explain this hashing funtion for strings within the C Programming forums, part of the General Programming Boards category; Code: unsigned int hashpjw(const void *key) { const char *ptr; unsigned int val; val=0; ptr=key; while (*ptr != '

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Can someone please explain this hashing funtion for strings

    Code:
    unsigned int hashpjw(const void *key) { 
    const char *ptr;
    unsigned int val;
    val=0;
    ptr=key;
    
    while (*ptr != '\0') { 
    unsigned int tmp; 
    val = (val << 4) + (*ptr); ;i dont understand this
    if (tmp = (val & 0xf0000000) ) {
    
    
    val = val ^ (tmp >> 24); val = val ^ tmp;
    } 
    ptr++;
    
    }
    return nevermind;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    val i left-shifted 4 places then the character at ptr is added. The problem is that val is uninitialized, and on the row below (val & 0xf0000000) is assigned to tmp not compared. Where did you find this?

    FWIW some hashing functions seemingly works by magic.
    Last edited by Subsonics; 04-12-2012 at 04:42 AM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Actually, forget what I just said, val is assigned 0 at the row below the declaration. And the test works in a similar manner as: if( (c = getchar()) == EOF ). Still, answering why a particular hashing function performs well (as in produces an even distribution) can be perplexing. I have been using djb2 for example which is a simple string hashing function but produces very even distribution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on using a funtion
    By Ambuter in forum C Programming
    Replies: 5
    Last Post: 10-27-2011, 03:10 PM
  2. funtion reuse
    By lukasjob in forum C Programming
    Replies: 6
    Last Post: 03-11-2011, 08:25 AM
  3. Can someone please explain strings to me?
    By themoffster in forum C Programming
    Replies: 17
    Last Post: 08-28-2003, 04:22 AM
  4. is this funtion ok?
    By kermit in forum C Programming
    Replies: 8
    Last Post: 08-22-2003, 05:28 PM
  5. Arguments in funtion - AGAIN
    By GaPe in forum C Programming
    Replies: 26
    Last Post: 09-30-2002, 04:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21