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

5 years ago Lalit Bhagtani 0

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

GETBIT Command

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

An error is returned,  if the key exists but value stored at the key is of the 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 :-

Redis GETBIT

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.

<- String Commands