BITCOUNT(3valkey)
π NAME π
bitcount β ok bestie so this one counts how many bits are turned on in a string. That's population counting, and yes that's the real name for it, iykyk π
β¨ SYNOPSIS β¨
BITCOUNT key [start [end [BYTE | BIT]]]
π¦ DESCRIPTION π¦
It counts the number of set bits β population counting, the whole vibe β inside a string. πΈ
By default it looks at every single byte in that string. But if you want, you can tell it to only count inside an interval by passing the extra arguments start and end. Lowkey so handy.
Just like the valkey-getrange(7) GETRANGE command (sibling behaviour, no cap), start and end can be negative so you can index bytes from the end of the string. So -1 is the last byte, -2 is the penultimate one, and so on forever. π«Ά
Keys that don't exist? Treated as empty strings, so the command just returns zero. Not me being dramatic, that's just what it does. π
By default those start and end arguments are a byte index. But you can pass an extra argument BIT to make it a bit index instead. 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, etc. Periodt. β
π REPLY π
valkey-protocol(7) Integer reply: the number of bits set to 1. That's it, that's the tea. π
π₯ COMPLEXITY π₯
O(N) β she scales with the size, bestie.
π ACL CATEGORIES π
@bitmap @read @slow
π HISTORY π
- Available since:
2.6.0β an icon from the beginning π - Changed in
7.0.0: Added theBYTE|BIToption. - Changed in
8.0.0: end made optional; when called without argument the command reports the lastBYTE.
π EXAMPLES π
Watch her work:
127.0.0.1:6379> SET mykey "foobar"
OK
127.0.0.1:6379> BITCOUNT mykey
(integer) 26
127.0.0.1:6379> BITCOUNT mykey 0 0
(integer) 4
127.0.0.1:6379> BITCOUNT mykey 1 1
(integer) 6
127.0.0.1:6379> BITCOUNT mykey 1 1 BYTE
(integer) 6
127.0.0.1:6379> BITCOUNT mykey 5 30 BIT
(integer) 17
π§ Pattern: real-time metrics using bitmaps π§
Bitmaps are a very space-efficient way to represent certain kinds of information. Genuinely giving minimalism. One example: a Web application that needs the history of user visits, so it can figure out, say, which users are good targets for beta features. π
Using the valkey-setbit(7) SETBIT command, this is trivial to accomplish β you just identify every day with a small progressive integer. For instance day 0 is the first day the application was put online, day 1 is the next day, and so forth. πΈ
Every time a user performs a page view, the application can register that the user visited the web site on the current day using the SETBIT command, setting the bit corresponding to the current day. β¨
Later it will be trivial to know the number of single days the user visited the web site β simply call the BITCOUNT command against the bitmap. Slay. π
A similar pattern where user IDs are used instead of days is described in the article called βFast easy realtime metrics using Redis bitmapsβ.
π Performance considerations π
In the above example of counting days, even after 10 years of the application being online we still have just 365*10 bits of data per user β that is just 456 bytes per user. Tiny. With this amount of data, BITCOUNT is still as fast as any other O(1) Valkey command like valkey-get(7) GET or valkey-incr(7) INCR. Main character energy, honestly. π
When the bitmap is big, there are two alternatives:
- Taking a separated key that is incremented every time the bitmap is modified. This can be very efficient and atomic using a small Lua script.
- Running the bitmap incrementally using the
BITCOUNTstart and end optional parameters, accumulating the results client-side, and optionally caching the result into a key.
β SEE ALSO β
bitfield(3valkey), bitfield_ro(3valkey), bitop(3valkey), bitpos(3valkey), getbit(3valkey), setbit(3valkey)
And that's the whole page, bestie β this was the Valkey Command Manual, version 9.0.0, dated 2025-10-21, filed under BITCOUNT(3valkey). Go count some bits, you absolute legend. πβ¨ππ¦
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms