SETBIT
Sets or clears the bit at offset (zero-indexed) in the string value stored at key.
The bit is either set or cleared depending on value, which can be either 0 or 1.
When key does not exist, a new string value is created. The string is grown to make sure it can hold a bit at offset. The offset argument is required to be greater than or equal to 0, and smaller than 232 (this limits bitmaps to 512MB). When the string at key is grown, added bits are set to 0.
Integer reply: the original bit value stored at offset.
There are cases when you need to set all the bits of single bitmap at once, for example when initializing it to a default non-zero value. It is possible to do this with multiple calls to the command, one for each bit that needs to be set. However, so as an optimization you can use a single SET
command to set the entire bitmap.
Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type (for more information refer to the ). This means that bitmaps can be used with string commands, and most importantly with SET
and .
For example, after setting a few bits, getting the string value of the bitmap would look like this:
By getting the string representation of a bitmap, the client can then parse the response’s bytes by extracting the bit values using native bit operations in its native programming language. Symmetrically, it is also possible to set an entire bitmap by performing the bits-to-bytes encoding in the client and calling SET
with the resultant string.
SETBIT
excels at setting single bits, and can be called several times when multiple bits need to be set. To optimize this operation you can replace multiple calls with a single call to the variadic BITFIELD
command and the use of fields of type u1
.