简体   繁体   中英

C how to combine string hash function with boolean?

I have a struct which contains a char* and a char which is always either 1 or 0. Here is my struct definition:

typedef struct Literal {
    char *name;
    char is_negated;
} Literal;

For string hashing I am using djb2 from http://www.cse.yorku.ca/~oz/hash.html :

size_t str_hash(void *string) {
    unsigned char *str = string;
    size_t hash = 5381;
    int c;
    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    }
    return hash;
}

I need to create a hash for my struct which will combine the information from the string hash with is_negated .

这是在不引入偏见的情况下做到这一点的方法:

return (str_hash(name) << 1) + is_negated;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM