BITOP

Redis Developer Course Redis Cloud Service Redis-Gate Server

Bitwise operation

Perform a bitwise operation between multiple keys (containing string values) and store the result in the destination key.
The BITOP command supports four bitwise operations: AND, OR, XOR and NOT.
operation AND, OR, XOR:
Usage is BITOP AND/OR/XOR destkey srckey1 srckey2 ... srckeyN.
operation NOT:
Usage is 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.

Return value

Integer reply: The size(bytes) of the string stored in the destination key, that is equal to the size of the longest input string.

Example

command>set key1 A
result>OK
command>set key2 B
result>OK
command>bitop AND destkey key1 key2
result>1
command>get destkey
result>@
command>bitop OR destkey key1 key2
result>1
command>get destkey
result>C
command>bitop XOR destkey key1 key2
result>1
command>get destkey
result>\x03   Return value from redis-cli  
command>bitop NOT destkey key1 key2
result>1
command>get destkey
result>\xbe   Return value from redis-cli  

Animation


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 slave (with read-only option disabled) where the bit-wise operations are performed to avoid blocking the master instance.


Syntax

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 :

Email 답글이 올라오면 이메일로 알려드리겠습니다.