BITCOUNT
The BITCOUNT
command in DiceDB is used to count the number of set bits (i.e., bits with value 1) in a string. This command is particularly useful for applications that need to perform bitwise operations and analyze binary data stored in DiceDB.
Syntax
- By default, the
start
andend
parameters are byte positions, but we can use an additional argument BIT to specify a bit position. - Negative values for
start
andend
are interpreted as offsets from the end of the string. For example, -1 means the last byte/bit, -2 means the second last byte/bit, and so on.
Parameters
Parameter | Description | Type | Required | Default |
---|---|---|---|---|
key | The key of the string for which the bit count is to be calculated. | String | Yes | |
start | The starting byte/bit position (inclusive) to count the bits. | Integer | No | 0 |
end | The ending byte/bit position (inclusive) to count the bits. | Integer | No | -1(end) |
BYTE | Use the specified range as byte indices | String | No | |
BIT | Use the specified ragne as bit indices | String | No |
Return Value
The BITCOUNT
command returns an integer representing the number of bits set to 1 in the specified range of the string.
Condition | Return Value |
---|---|
Command is successful | number of bits set to 1 in the specified range of the string |
Non-existent key | 0 |
Out of range indices | no. of set bits based on the valid range (if any) inside the specified range |
Invalid syntax / wrong key type | error |
Behaviour
When the BITCOUNT
command is executed, DiceDB will:
- Retrieve the string stored at the specified key.
- If the
start
andend
parameters are provided, DiceDB will consider only the specified range of bytes/bits within the string. - Whether it’s the byte index or a bit index would be decided based on the optional parameter BYTE or BIT. (The default is BYTE)
- Count the number of bits set to 1 within the specified range or the entire string if no range is specified.
- Return the count as an integer.
Errors
-
Non-existent Key
:- Error Message: None (treats it as an empty string and returns 0)
- Occurs if the specified key does not exist
-
Wrong Type of Key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs if the key exists but does not hold a string value
- Error Message:
-
Invalid Syntax / Range
:- Error Message:
(error) ERR value is not an integer or out of range
- Occurs if the
start
orend
parameters are not integers
- Error Message:
-
Out of Range Indices
:- Error Message: None (will be handled gracefully by considering the valid range within the string)
- Occurs if the
start
orend
parameters are out of range of the string length
Example Usage
Example 1: Counting bits in the entire string
Example 2: Counting bits in a specified range
In this example, the command counts the bits set to 1 in the bytes from position 1 to 3 (inclusive) of the string “foobar”.
Example 3: Counting bits in a specified range using bit indices
In this example, the command counts the bits set to 1 in the bit ranges from position 8 to 31 (inclusive) of the string “foobar”.