bitmap

BITCOUNT

  1. 对字符串中设置的位数(总体计数)进行计数。(计数1的数量)

  2. 语法:

BITCOUNT key [start end [BYTE | BIT]]
  1. 示例:
redis> SET mykey "foobar"
"OK"
redis> BITCOUNT mykey
(integer) 26
redis> BITCOUNT mykey 0 0
(integer) 4
redis> BITCOUNT mykey 1 1
(integer) 6
redis> BITCOUNT mykey 1 1 BYTE
(integer) 6
redis> BITCOUNT mykey 5 30 BIT
(integer) 17

BITFIELD

  1. 对字符串执行任意位域整数操作。

  2. 语法:

BITFIELD key [GET encoding offset | [OVERFLOW <WRAP | SAT | FAIL>]
    <SET encoding offset value | INCRBY encoding offset increment>
    [GET encoding offset | [OVERFLOW <WRAP | SAT | FAIL>]
    <SET encoding offset value | INCRBY encoding offset increment>
    ...]]
  1. 用法:
> BITFIELD mykey INCRBY i5 100 1 GET u4 0
1) (integer) 1
2) (integer) 0
> BITFIELD mykey incrby u2 100 1 OVERFLOW SAT incrby u2 102 1
3) (integer) 1
4) (integer) 1
> BITFIELD mykey incrby u2 100 1 OVERFLOW SAT incrby u2 102 1
5) (integer) 2
6) (integer) 2
> BITFIELD mykey incrby u2 100 1 OVERFLOW SAT incrby u2 102 1
7) (integer) 3
2) (integer) 3
> BITFIELD mykey incrby u2 100 1 OVERFLOW SAT incrby u2 102 1
3) (integer) 0
4) (integer) 3

BITFIELD_RO

  1. 对字符串执行任意只读位域整数操作。

  2. 语法:

BITFIELD_RO key [GET encoding offset [GET encoding offset ...]]
  1. 示例:
BITFIELD_RO hello GET i8 16

BITOP

  1. 对多个字符串执行按位操作,并存储结果。

  2. 语法:

BITOP <AND | OR | XOR | NOT> destkey key [key ...]
  1. 用法:
redis> SET key1 "foobar"
"OK"
redis> SET key2 "abcdef"
"OK"
redis> BITOP AND dest key1 key2
(integer) 6
redis> GET dest
"`bc`ab"

BITPOS

  1. 查找字符串中的第一个set(1)或clear(0)位。

  2. 语法:

BITPOS key bit [start [end [BYTE | BIT]]]
  1. 用法:
redis> SET mykey "\xff\xf0\x00"
"OK"
redis> BITPOS mykey 0
(integer) 0
redis> SET mykey "\x00\xff\xf0"
"OK"
redis> BITPOS mykey 1 0
(integer) 1
redis> BITPOS mykey 1 2
(integer) 18
redis> BITPOS mykey 1 2 -1 BYTE
(integer) 18
redis> BITPOS mykey 1 7 15 BIT
(integer) 9
redis> set mykey "\x00\x00\x00"
"OK"
redis> BITPOS mykey 1
(integer) 1
redis> BITPOS mykey 1 7 -3 BIT
(integer) 9

GETBIT

  1. 按偏移量返回位值。

  2. 语法:

GETBIT key offset
  1. 示例:
redis> SETBIT mykey 7 1
(integer) 0
redis> GETBIT mykey 0
(integer) 0
redis> GETBIT mykey 7
(integer) 1
redis> GETBIT mykey 100
(integer) 0

SETBIT

  1. 设置或清除字符串值的偏移位。如果key不存在,则创建该key。

  2. 语法:

SETBIT key offset value
  1. 示例:
redis> SETBIT mykey 7 1
(integer) 0
redis> SETBIT mykey 7 0
(integer) 1
redis> GET mykey
""