Redis BITCOUNT 行为

Redis BITCOUNT behavior

redis bitcount 命令如何工作?

setbit test 1 1
setbit test 3 1

计数位。

bitcount test 

returns 2

范围计数。

bitcount test 1 2

returns 0

为什么?我想我应该为 1 和 2 之间的位位置取 1,我们只设置了一位。

startend 参数指的是 字节 。所以你要问字符串的第二个和第三个字节中的位数。您只在第一个字节中设置了位,所以答案是 0.

不幸的是 BITCOUNT doesn't explain that, but it is mentioned in the documentation for BITPOS 的文档:

It is possible to look for bits only in a specified interval passing the additional arguments start and end.... The range is interpreted as a range of bytes and not a range of bits, so start=0 and end=2 means to look at the first three bytes.