> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# BITFIELD

> Sets or gets parts of a bitfield

The `bitfield` function returns a `BitFieldCommands` instance that can be used
to execute multiple bitfield operations in a single command.

The encoding can be a signed or unsigned integer, by prefixing the type with
`i` or `u`. For example, `i4` is a signed 4-bit integer, and `u8` is an
unsigned 8-bit integer.

```py theme={"system"}
redis.set("mykey", "")

# Sets the first 4 bits to 1
# Increments the next 4 bits by 1
result = redis.bitfield("mykey")
        .set("u4", 0, 16)
        .incr("u4", 4, 1)
        .execute()

assert result == [0, 1]
```

## Commands

### `get(type: str, offset: int)`

Returns a value from the bitfield at the given offset.

### `set(type: str, offset: int, value: int)`

Sets a value and returns the old value.

### `incr(type: str, offset: int, increment: int)`

Increments a value and returns the new value.

## Arguments

<ParamField body="key" type="str" required>
  The string key to operate on.
</ParamField>

## Response

<ResponseField type="List[int]" required>
  A list of integers, one for each operation.
</ResponseField>

<RequestExample>
  ```py Get theme={"system"}
  redis.set("mykey", "\x05\x06\x07")

  result = redis.bitfield("mykey") \
      .get("u8", 0) \
      .get("u8", 8) \
      .get("u8", 16) \
      .execute()

  assert result == [5, 6, 7]
  ```

  ```py Set theme={"system"}
  redis.set("mykey", "")

  result = redis.bitfield("mykey") \
      .set("u4", 0, 16) \
      .set("u4", 4, 1) \
      .execute()
    
  assert result == [0, 1]
  ```

  ```py Incr theme={"system"}
  redis.set("mykey", "")

  # Increment offset 0 by 16, return 
  # Increment offset 4 by 1

  result = redis.bitfield("mykey") \
      .incr("u4", 0, 16) \
      .incr("u4", 4, 1) \
      .execute()

  assert result == [0, 1]
  ```
</RequestExample>
