w3resource

Redis String: SETBIT

Redis SETBIT Command

The Redis SETBIT key is used to sets or clears the bit at offset in the string value stored at key. It is depending on value, which can be either 0 or 1. When the 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.

Here are some common uses and scenarios for the "SETBIT" command:

  • Bitfield Manipulation: Directly modify individual bits within a string for bitwise operations.
  • Flags and States: Manage and update boolean flags or states within a compact bitfield.
  • Custom Data Structures: Implement custom data structures that use bits for storage efficiency.
  • Bitmap Indexing: Create and update bitmap indexes for efficient querying and indexing.
  • Statistical Counters: Track occurrences or events using individual bits for compact storage.

Syntax:

SETBIT KEY_NAME OFFSET

Available since

2.2.0.

Return Value

Integer reply: the original bit value stored at offset.

Return Value Type

Integer

Example: Redis SETBIT

First, set a key in redis and then get it.

redis 127.0.0.1:6379> SETBIT mykey 7 1
(integer) 0
redis 127.0.0.1:6379> GETBIT mykey 0
(integer) 0
redis 127.0.0.1:6379> GETBIT mykey 7
(integer) 1
redis 127.0.0.1:6379> GETBIT mykey 100
(integer) 0

Previous: MGET
Next: SETEX



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-setbit-key-offset-value.php