Use SETBIT to set a single bit of the string stored at a key to 0 or 1.
The offset is counted in bits from the start of the value and the reply is the bit's previous value. If the key does not exist, or the offset lies past the end of the current value, the string is first extended with zero bits, so writing to a large offset allocates every byte up to it. Keep offsets dense, for example by mapping each user to a small sequential id.
Bitmaps built this way are read back with GETBIT, counted with BITCOUNT, and combined with BITOP.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
<offset> | Yes | No | Zero-based bit offset to write. |
<value> | Yes | No | Bit value to write: 0 or 1. |
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Integer: the previous bit at the offset, 0 or 1 |
| RESP3 | Integer: the previous bit at the offset, 0 or 1 |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.