Redis GETBIT – How to get bit value at specific index of string in redis

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get a bit value at the specified index of the string value stored at a key in redis datastore. For this we will use redis GETBIT command. 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. 

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

Syntax :-

redis host:post> GETBIT <keyname> <index>

Output :-

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

Example :-


References :-

  1. GETBIT Command Docs

That’s all for how to get 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.