关于redis位图结构内存​​存储问题

About redis bitmap structure memory storage problem

例如

> SETBIT bitmapsarestrings 2 1
> SETBIT bitmapsarestrings 3 1
> SETBIT bitmapsarestrings 5 1
> SETBIT bitmapsarestrings 10 1
> SETBIT bitmapsarestrings 11 1
> SETBIT bitmapsarestrings 14 1
> GET bitmapsarestrings
"42"

二进制存储不应该是这样的:0010 0110 0001 1100 ?

这样存储,为什么值为42?

这些SETBIT操作将使值成为二进制字符串,其长度为2个字节或16位。设置后,二进制格式为0b 00110100 00110010

第一个字节(0b 00110100)是52,是'4'的ascii码,第二个字节(0b 00110010)是50,这是'2'的ascii码。所以当你得到字符串的值时,它 returns "42".

@for_stack 所说的内容,或仅参考该示例 (https://redis.io/commands/setbit) 上方的行:

Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type (for more information refer to the Bitmaps section of the Data Types Introduction page). This means that bitmaps can be used with string commands, and most importantly with SET and GET.

Because Redis' strings are binary-safe, a bitmap is trivially encoded as a bytes stream. The first byte of the string corresponds to offsets 0..7 of the bitmap, the second byte to the 8..15 range, and so forth.