BITOP(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’… 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 ... srckeyN
  • BITOP OR destkey srckey1 srckey2 srckey3 ... srckeyN
  • BITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyN
  • BITOP 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 πŸ”₯

BITOP is a potentially slow command because she runs in O(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