Redis Bitmap BITCOUNT Command
The Redis BITCOUNT
command count the number of set bits (population counting) in a string.
Syntax
The basic syntax of the BITCOUNT
command is as follows:
1 | BITCOUNT key [start end [BYTE | BIT]] |
By default all the bytes contained in the string are examined. It is possible to specify the counting operation only in an interval passing the additional arguments start and end.
Like for the GETRANGE
command start and end can contain negative values in order to index bytes starting from the end of the string, where -1 is the last byte, -2 is the penultimate, and so forth.
Non-existent keys are treated as empty strings, so the command will return zero.
By default, the additional arguments start and end specify a byte index. We can use an additional argument BIT to specify a bit index. So 0 is the first bit, 1 is the second bit, and so forth. For negative values, -1 is the last bit, -2 is the penultimate, and so forth.
Note: Starting with Redis version 7.0.0: Added the BYTE|BIT option.
Available Since
Redis version >= 2.6.0
Time Complexity
O(N)
ACL Categories
@read
, @bitmap
, @slow
Return Value
Returns the number of bits set to 1.
Example
1 | 127.0.0.1:6379> SET mykey JOHNSONLIN |