|Redis Developer Course||Redis Cloud Service||Redis-Gate Server|
Perform a bitwise operation between multiple keys (containing string values)
and store the result in the destination key.
BITOP command supports four bitwise operations: AND, OR, XOR and NOT.
operation AND, OR, XOR:
BITOP AND/OR/XOR destkey srckey1 srckey2 ... srckeyN.
BITOP NOT destkey srckey.
As you can see NOT is special as it only takes an input key, because it performs inversion of bits so it only makes sense as an unary operator.
The result of the operation is always stored at destkey.
Integer reply: The size(bytes) of the string stored in the destination key, that is equal to the size of the longest input string.
|command>||set key1 A|
|command>||set key2 B|
|command>||bitop AND destkey key1 key2|
|command>||bitop OR destkey key1 key2|
|command>||bitop XOR destkey key1 key2|
|result>||\x03 Return value from redis-cli|
|command>||bitop NOT destkey key1 key2|
|result>||\xbe Return value from redis-cli|
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 slave (with read-only option disabled) where the bit-wise operations are performed to avoid blocking the master instance.
BITOP operation destkey srckey1 [srckey2 ...]
- Available since 2.2.0.
- Time complexity(Logical processing time): O(N)
|Related Commands||SETBIT, GETBIT, BITCOUNT, BITPOS|
|Clients for Java||Jedis, Lettuce, Redisson||Clients for C||Hiredis|
To practice the command, click here to run Redis Web Client
|<< GETBIT||BITOP||BITCOUNT >>|
Click for a question or to see comments. Comments : Viewed :