BITOP(3valkey)
π BITOP β the girlies are doing bitwise math on your strings, bestie
okay so this is BITOP(3valkey), and honestly? she's giving main character energy. She takes a bunch of string keys, does a whole bitwise operation on them, and drops the result into a destination key. Iconic. Let's go. β¨
β¨ SYNOPSIS β¨
BITOP <AND | OR | XOR | NOT> destkey key [key...]
π¦ DESCRIPTION π¦
So basically you're performing a bitwise operation between multiple keys (and those keys are holding string values, no cap) and then storing the result in the destination key. That's the whole vibe.
BITOP supports four bitwise operations: AND, OR, XOR and NOT. So the valid ways to call her are:
BITOP AND destkey srckey1 srckey2 srckey3 ... srckeyNBITOP OR destkey srckey1 srckey2 srckey3 ... srckeyNBITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyNBITOP NOT destkey srckey
And yes, NOT is the special one π₯Ί β she only takes one input key, because she flips all the bits, so she literally only makes sense as a unary operator. She's an only child, she doesn't share.
The result of the operation is always stored at destkey. Always. No exceptions. Periodt. π
πΈ Handling of strings with different lengths πΈ
When you run an operation between strings that have different lengths, every string shorter than the longest string in the set gets treated like it was zero-padded up to the length of the longest string. So she's not gonna yell at you about mismatched lengths, she just pads and keeps it moving.
Same energy for keys that don't even exist β they get treated as a whole stream of zero bytes, up to the length of the longest string. Ghost keys still show up to the function, they just show up as zeroes. π
π REPLY π
valkey-protocol(7) Integer reply: the size of the string stored in the destination key, and that size is equal to the size of the longest input string. She tells you how big the result is, and it's as big as her biggest input. Obsessed.
β‘ COMPLEXITY β‘
O(N)
π ACL CATEGORIES π
@bitmap @slow @write
π HISTORY π
- Available since:
2.6.0
π EXAMPLES π
127.0.0.1:6379> SET key1 "foobar"
OK
127.0.0.1:6379> SET key2 "abcdef"
OK
127.0.0.1:6379> BITOP AND dest key1 key2
(integer) 6
127.0.0.1:6379> GET dest
"`bc`ab"
look at that little "bcab" just sitting there. she ATE. π
π§ Pattern: real time metrics using bitmaps π§
BITOP is a great lil complement to the pattern documented in the valkey-bitcount(7) BITCOUNT command documentation. You can combine different bitmaps together to get a target bitmap, and then you run the population counting operation on that. It's giving collab. It's giving teamwork.
There's an article called "Fast easy realtime metrics using Valkey bitmaps" if you want to see interesting use cases β go read it, bestie, iykyk. πβ¨
π₯ Performance considerations π₯
BITOPis a potentially slow command because she runs inO(N)time. So be careful when you run her against long input strings. She will take her time and she will block things while she does it. π
For real-time metrics and statistics involving large inputs, a good approach is to use a replica (with the replica-read-only option enabled) and do the bit-wise operations there, so you avoid blocking the primary instance. Protect the primary at all costs, she's busy. π«Ά
π SEE ALSO π
bitcount(3valkey), bitfield(3valkey), bitfield_ro(3valkey), bitpos(3valkey), getbit(3valkey), setbit(3valkey)
and that's the whole page, bestie!! this was version 9.0.0, dated 2025-10-21, and yes she really is called BITOP(3valkey) β go forth and bitwise, mwah πβ¨π
π¦ 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