[フレーム]
Docs Pricing
Login Book a meeting Try Redis

BITOP

Syntax
BITOP <AND | OR | XOR | NOT | DIFF | DIFF1 | ANDOR | ONE> destkey key [key ...]
Available since:
Redis Open Source 2.6.0
Time complexity:
O(N)
ACL categories:
@write, @bitmap, @slow,
Compatibility:
Redis Enterprise and Redis Cloud compatibility

Perform a bitwise operation between multiple keys (containing string values) and store the result in the destination key.

The BITOP command supports eight bitwise operations: AND, OR, XOR, NOT, DIFF, DIFF1, ANDOR, and ONE. The valid forms to call the command are:

The result of each operation is always stored at destkey.

  1. Added in Redis 8.2.

Handling of strings with different lengths

When an operation is performed between strings having different lengths, all the strings shorter than the longest string in the set are treated as if they were zero-padded up to the length of the longest string.

The same holds true for non-existent keys, that are considered as a stream of zero bytes up to the length of the longest string.

Examples

  1. Basic usage example using the AND operator:
BITFIELD key1 SET i8 #0 255 BITFIELD key2 SET i8 #0 85 BITOP AND dest key1 key2 BITFIELD dest GET i8 #0
  1. Suppose you want to expose people to a book-related ad. The target audience is people who love to read books and are interested in fantasy, adventure, or science fiction. Assume you have the following bitmaps:

To create a bitmap representing the target audience, use the following command:

BITOP ANDOR TA LRB B:F B:A B:SF

Pattern: real time metrics using bitmaps

BITOP is a good complement to the pattern documented in the BITCOUNT command documentation. Different bitmaps can be combined in order to obtain a target bitmap where the population counting operation is performed.

See the article called "Fast easy realtime metrics using Redis bitmaps" for an interesting use cases.

Performance considerations

BITOP is a potentially slow command as it runs in O(N) time. Care should be taken when running it against long input strings.

For real-time metrics and statistics involving large inputs a good approach is to use a replica (with replica-read-only option enabled) where the bit-wise operations are performed to avoid blocking the master instance.

Redis Enterprise and Redis Cloud compatibility

Redis
Enterprise
Redis
Cloud
Notes
✅ Standard
✅ Active-Active
✅ Standard
✅ Active-Active

Return information

Integer reply: the size of the string stored in the destination key is equal to the size of the longest input string.
RATE THIS PAGE
Back to top ↑

AltStyle によって変換されたページ (->オリジナル) /