簡體   English   中英

C如何將字符串哈希函數與布爾值結合起來?

[英]C how to combine string hash function with boolean?

我有一個結構,其中包含一個 char* 和一個始終為 1 或 0 的 char。這是我的結構定義:

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

對於字符串散列,我使用來自http://www.cse.yorku.ca/~oz/hash.html 的djb2 :

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;
}

我需要為我的結構創建一個哈希,它將字符串哈希中的信息與is_negated結合起來。

這是在不引入偏見的情況下做到這一點的方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM