GETBIT
The GETBIT
command is used to retrieve the bit value at a specified offset in the string value stored at a given key. This command is particularly useful for bitwise operations and managing binary data within DiceDB.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key of the string from which the bit value is to be retrieved. This key must reference a string value. | String | Yes |
offset | The position of the bit to retrieve. The offset is a zero-based integer, meaning the first bit is at position 0. | Integer | Yes |
Return Values
Condition | Return Value |
---|---|
Command is successful | 0 or 1 |
Syntax or specified constraints are invalid | error |
Behaviour
- Check if the specified key exists.
- If the key does not exist, it is treated as if it contains a string of zero bytes, and the bit at any offset will be
0
. - If the key exists but does not hold a string value, an error is returned.
- If the key exists and holds a string value, the bit at the specified offset is retrieved and returned.
- If the key exists and holds a string value, however the offset is more than string length,
0
will be returned.
Errors
-
Wrong number of arguments
:- Error Message:
(error) wrong number of arguments for 'GETBIT' command
- Occurs if both key and offset are not provided.
- Error Message:
-
Non-string value stored against the key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs if the key exists but does not contain a string value.
- Error Message:
-
Non-integer or negative value for offset
:- Error Message:
(error) ERR bit offset is not an integer or out of range
- Occurs if the offset is not a valid integer or is negative.
- Error Message:
Example Usage
Basic Usage
Setting a key foo
with the value a
(ASCII value of a
is 97
, represented as 01100001
in binary). Then retrieve the bit value at index 1
.
Key does not exist
Trying to retrieve bit value from a non-existent key.
Key holds a non-string value
Setting a key baz
with a list of items and retrieving bit value from the key.
Invalid offset
Setting a key foo
with the value a
and trying to retrieve non-integer and negative bit value.
Insufficient parameters
Trying to execute GETBIT
command without offset
argument.
Notes
- The
GETBIT
command operates on the raw binary representation of the string. This means that the offset is counted in bits, not bytes. - The maximum offset that can be specified is
2^32 - 1 (4294967295)
, as DiceDB strings are limited to512 MB
.