BITOP
The BITOP
command in DiceDB is used to perform bitwise operations between strings. This command supports several bitwise operations such as AND, OR, XOR, and NOT. The result of the operation is stored in a destination key.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
AND | Perform a bitwise AND operation. | Operation | Yes |
OR | Perform a bitwise OR operation. | Operation | Yes |
XOR | Perform a bitwise XOR operation. | Operation | Yes |
NOT | Perform a bitwise NOT operation (only one key is allowed for this operation). | Operation | Yes |
destkey | The key where the result of the bitwise operation will be stored. | String | Yes |
key [key ...] | One or more keys containing the strings to be used in the bitwise operation. For the NOT operation, only one key is allowed. | String | Yes |
Return Value
Condition | Return Value |
---|---|
Integer | The command returns an integer value representing the length of the resulting string. |
Error | An error is returned if the command fails. |
Behaviour
When the BITOP
command is executed, it performs the specified bitwise operation on the provided keys and stores the result in the destination key. The length of the resulting string is determined by the longest input string. If the input strings are of different lengths, the shorter strings are implicitly padded with zero-bytes to match the length of the longest string.
Bitwise Operations
AND
: Each bit in the result is set to 1 if the corresponding bits of all input strings are 1. Otherwise, it is set to 0.OR
: Each bit in the result is set to 1 if at least one of the corresponding bits of the input strings is 1. Otherwise, it is set to 0.XOR
: Each bit in the result is set to 1 if an odd number of the corresponding bits of the input strings are 1. Otherwise, it is set to 0.NOT
: Each bit in the result is set to the complement of the corresponding bit in the input string (only one input string is allowed).
Error Handling
The BITOP
command can raise errors in the following cases:
-
Wrong number of arguments
: If the number of arguments is incorrect, DiceDB will return an error.Error Message
:ERR wrong number of arguments for 'bitop' command
-
Invalid operation
: If the specified operation is not one ofAND
,OR
,XOR
, orNOT
, DiceDB will return an error.Error Message
:ERR syntax error
-
Invalid number of keys for NOT operation
: If more than one key is provided for theNOT
operation, DiceDB will return an error.Error Message
:ERR BITOP NOT must be called with a single source key
-
Non-string keys
: If any of the provided keys are not strings, DiceDB will return an error.Error Message
:WRONGTYPE Operation against a key holding the wrong kind of value
Example Usage
Example 1: Bitwise AND Operation
Explanation
:
SET key1 "foo"
: Sets the value ofkey1
to “foo”.SET key2 "bar"
: Sets the value ofkey2
to “bar”.BITOP AND result key1 key2
: Performs a bitwise AND operation between the values ofkey1
andkey2
, and stores the result inresult
.GET result
: Retrieves the value ofresult
.
Example 2: Bitwise OR Operation
Explanation
:
SET key1 "foo"
: Sets the value ofkey1
to “foo”.SET key2 "bar"
: Sets the value ofkey2
to “bar”.BITOP OR result key1 key2
: Performs a bitwise OR operation between the values ofkey1
andkey2
, and stores the result inresult
.GET result
: Retrieves the value ofresult
.
Example 3: Bitwise XOR Operation
Explanation
:
SET key1 "foo"
: Sets the value ofkey1
to “foo”.SET key2 "bar"
: Sets the value ofkey2
to “bar”.BITOP XOR result key1 key2
: Performs a bitwise XOR operation between the values ofkey1
andkey2
, and stores the result inresult
.GET result
: Retrieves the value ofresult
.
Example 4: Bitwise NOT Operation
Explanation
:
SET key1 "foo"
: Sets the value ofkey1
to “foo”.BITOP NOT result key1
: Performs a bitwise NOT operation on the value ofkey1
, and stores the result inresult
.GET result
: Retrieves the value ofresult
.