如何在Python中使用64位无符号整数数学,这与C溢出有关?

2024-04-19 22:45:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python实现djb2散列。你知道吗

这里是C:

/* djb2 hash http://www.cse.yorku.ca/~oz/hash.html */

uint64_t djb2(size_t len, char const str[len]) {
    uint64_t hash = 5381;
    uint8_t c;
    for(size_t i = 0; i < len; i++) {
        c = str[i];
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    }
    return hash;
}

下面是我对Python的尝试:

from ctypes import c_uint64, c_byte, cast, POINTER

def djb2(string: str) -> c_uint64:
    hash = c_uint64(5381)
    raw_bytes = cast(string, POINTER(c_byte * len(string)))[0]
    for i in range(0, len(raw_bytes)):
        hash = c_uint64((((((hash.value << 5) & 0xffffffffffffffff) + hash.value) & 0xffffffffffffffff) + raw_bytes[i]) & 0xffffffffffffffff) # hash * 33 + c
    return hash

然而,我得到了两个不同的结果,我怀疑这是因为不同的溢出行为,或者数学上的差异。你知道吗

python版本中屏蔽的原因是试图强制溢出(基于this answer)。你知道吗


Tags: forsizestringrawlenreturnbytesvalue
1条回答
网友
1楼 · 发布于 2024-04-19 22:45:02

您可以在纯Python中非常轻松地实现由C代码运行的算法,而不需要任何ctypes内容。只需使用常规的Python整数,并在末尾取一个模(对于正在执行的操作,高位不会影响低位):

def djb2(string: bytes) -> int:  # note, use a bytestring for this, not a Unicode string!
    h = 5381
    for c in string:    # iterating over the bytestring directly gives integer values
        h = h * 33 + c  # use the computation from the C comments, but consider ^ instead of +
    return h % 2**64    # note you may actually want % 2**32, as this hash is often 32-bit

正如我在代码中所评论的,由于这是一个在bytestrings上定义的操作,所以应该使用bytes实例作为参数。注意这个算法有很多不同的实现。有些人在更新散列值的步骤中使用^(按位异或)而不是+,而且通常定义为使用unsigned long,通常是32位,而不是问题中C版本使用的显式64位整数。你知道吗

相关问题 更多 >