SETBIT

Introduction and Use Case(s)

The SETBIT command in Redis is used to set or clear the bit at a specified offset in the string value stored at a key. It’s typically used for bit manipulation tasks, such as managing bitmap indices, feature flags, or tracking user activity.

Syntax

1. SETBIT key offset value

Parameter Explanations

  • key: The name of the key where the string value is stored.
  • offset: The position (starting from 0) within the string to set or clear the bit.
  • value: The bit value to set, which must be either 0 or 1.

Return Values

SETBIT returns the original bit value stored at the specified offset before it was set.

Examples:

  1. Setting a new bit:

  2. If the bit was originally 0: (integer) 0

  3. If the bit was originally 1: (integer) 1
  4. Clearing a bit:

  5. If the bit was originally 0: (integer) 0

  6. If the bit was originally 1: (integer) 1

Code Examples

1. dragonfly> SETBIT mykey 7 1
2. (integer) 0

4. dragonfly> SETBIT mykey 7 0
5. (integer) 1

7. dragonfly> SETBIT mykey 7 1
8. (integer) 0

10. dragonfly> GETBIT mykey 7
11. (integer) 1

Best Practices

When working with large offsets, ensure that memory usage is controlled, as setting a high offset can lead to increased memory consumption.

Common Mistakes

  • Using non-integer values for offset.
  • Setting value to anything other than 0 or 1, which will result in an error.

FAQs

What happens if the key does not exist?

If the key does not exist, it is treated as a zero-length string, so SETBIT will extend it as needed and initialize unspecified bits to 0.

Can I use negative offsets?

No, the offset parameter must be a non-negative integer.