Redis SETBIT – How to set bit value at specific index of string in redis

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to set a bit value at the specified index of the string value stored at a key in redis datastore. For this we will use redis SETBIT command.

SETBIT Command

This command set a bit value at the specified index of the string value stored at a key. If index is greater than the length of the string value, then string is considered as a contiguous space with 0 bits. If the key does not exist, then it is first created and set to a empty string. Hence index will always be out of range and its value will be 0 bit.

An error is returned,  if the key exist but value stored at the key is of wrong datatype. The syntax of redis SETBIT command is as follows :-

Syntax :-

redis host:post> SETBIT <keyname> <index> <bit value>

Output :-

- (integer) reply, representing the old bit value stored at index.
- Error, if key exist and value stored at the key is not a string.

Example :-

Redis SETBIT

 

References :-

  1. SETBIT Command Docs

That’s all for how to set a bit value at the specific index of the string value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.

<- String Commands