BITCOUNT

Redis Developer Course Redis Cloud Service Redis-Gate Server

Return the number of set bits in a string.

Set bit is 1.   The offsets start and end are byte positions. It is not a bit position.   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.
Usage is BITCOUNT key start end.

Return value

Integer reply: The number of bits set to 1.

Example

command>set key ABC
result>OK
command>bitcount key
result>7
command>bitcount key 1 2
result>5

Animation


Performance considerations

In a example of counting days, even after 10 years the application is online we still have just 365*10 bits of data per user, that is just 456 bytes per user.   With this amount of data BITCOUNT is still as fast as any other O(1) Redis command like GET or INCR.


Syntax

BITCOUNT key [start end]

  • Available since 2.6.0.
  • Time complexity(Logical processing time): O(N)
Related Commands SETBIT, GETBIT, BITOP, BITPOS
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

To practice the command, click here to run Redis Web Client


<< BITOP BITCOUNT BITPOS >>

Click for a question or to see comments.  Comments :    Viewed :

Email 답글이 올라오면 이메일로 알려드리겠습니다.