一个字节是8个位,例如data,是四个字节,也就是32个位,可以把Bitmaps想象成一个以位为单位的数组,数组的每个单元只能存储0和1,数组的下标在Bitmaps中叫做偏移量。
设置值
setbit key offset value
获取值
getbit key offset
获取Bitmaps指定范围值为1的个数
bitcount key start end
获取key中从start字节开始,到end字节中,所包含的位为1的个数。
Bitmaps间的运算
bitop op key1 key2
op可以为:
- and:交集
- or:并集
- not:非
- xor:异或
计算Bitmaps中第一个值为targetBit的偏移量
bitops key targetBit start end
Bitmaps使用场景
上面有很多可以进行位操作的命令,他的典型使用场景为,计算网站的独立访问的用户,如果使用集合的话,会占用很大的内存,这时使用Bitmaps刚好。
但是假如该网站每天的独立访问用户很少,使用Bitmaps就不太适合了。