> ## 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.

# BITOP

> Perform bitwise operations between strings.

The `BITOP` command in Redis is used to perform bitwise operations on multiple keys (or Redis strings) and store the result in a destination key. It supports standard logical operations (AND, OR, XOR, NOT) and advanced operations (DIFF, DIFF1, ANDOR, ONE) for complex bit manipulation.

## Arguments

<ParamField body="operation" type="'and' | 'or' | 'xor' | 'not' | 'diff' | 'diff1' | 'andor' | 'one'" required>
  Specifies the type of bitwise operation to perform:

  **Standard Operations:**

  * `and`: Bitwise AND
  * `or`: Bitwise OR
  * `xor`: Bitwise XOR
  * `not`: Bitwise NOT (only accepts one source key)

  **Advanced Operations (Redis 8.2+):**

  * `diff`: A bit is set only if it's set in all source bitmaps
  * `diff1`: A bit is set if it's set in the first key but not in any of the other keys
  * `andor`: A bit is set if it's set in X and also in one or more of Y1, Y2, ...
  * `one`: A bit is set if it's set in exactly one source key
</ParamField>

<ParamField body="destinationKey" type="string" required>
  The key to store the result of the operation in.
</ParamField>

<ParamField body="sourceKeys" type="...string[]" required>
  One or more keys to perform the operation on.
</ParamField>

## Response

<ResponseField type="integer" required>
  The size of the string stored in the destination key.
</ResponseField>

<RequestExample>
  ```ts Standard Operations theme={"system"}
  // AND operation
  await redis.bitop("and", "destKey", "sourceKey1", "sourceKey2");

  // OR operation
  await redis.bitop("or", "destKey", "sourceKey1", "sourceKey2");

  // XOR operation
  await redis.bitop("xor", "destKey", "sourceKey1", "sourceKey2");

  // NOT operation (only accepts one source key)
  await redis.bitop("not", "destKey", "sourceKey");
  ```

  ```ts DIFF Operation (Redis 8.2+) theme={"system"}
  // Set bits in multiple bitmaps
  await redis.setbit("bitmap1", 0, 1);
  await redis.setbit("bitmap1", 1, 1);
  await redis.setbit("bitmap2", 1, 1);
  await redis.setbit("bitmap2", 2, 1);

  // DIFF: bits set in all sources
  await redis.bitop("diff", "result", "bitmap1", "bitmap2");
  // Result: bit 1 is set (only bit present in both)
  ```

  ```ts DIFF1 Operation (Redis 8.2+) theme={"system"}
  // Set bits in bitmaps
  await redis.setbit("exclude", 0, 1);
  await redis.setbit("exclude", 1, 1);
  await redis.setbit("include1", 1, 1);
  await redis.setbit("include2", 2, 1);

  // DIFF1: bits in first key but not in others
  await redis.bitop("diff1", "result", "exclude", "include1", "include2");
  // Result: bit 0 is set (only in exclude, not in include1 or include2)
  ```

  ```ts ANDOR Operation (Redis 8.2+) theme={"system"}
  // Set bits in bitmaps
  await redis.setbit("required", 0, 1);
  await redis.setbit("required", 1, 1);
  await redis.setbit("option1", 1, 1);
  await redis.setbit("option2", 2, 1);

  // ANDOR: bits in required AND in at least one option
  await redis.bitop("andor", "result", "required", "option1", "option2");
  // Result: bit 1 is set (in required AND in option1)
  ```

  ```ts ONE Operation (Redis 8.2+) theme={"system"}
  // Set bits in multiple sources
  await redis.setbit("source1", 0, 1);
  await redis.setbit("source1", 1, 1);
  await redis.setbit("source2", 1, 1);
  await redis.setbit("source2", 2, 1);
  await redis.setbit("source3", 2, 1);

  // ONE: bits set in exactly one source
  await redis.bitop("one", "result", "source1", "source2", "source3");
  // Result: bit 0 is set (only in source1)
  ```
</RequestExample>

## Advanced Operations Details (Redis 8.2+)

### DIFF

A bit in the destination is set if it is set in **all** source bitmaps. This is equivalent to the intersection of all source bitmaps.

**Use Cases:**

* Finding common features across multiple datasets
* Identifying users present in all segments
* Intersection of multiple filters

### DIFF1

A bit in the destination is set if it is set in the **first key** but **not in any of the other keys**. This finds bits unique to the first bitmap.

**Use Cases:**

* Finding exclusive features
* Identifying users in one segment but not in others
* Exclusion filtering

### ANDOR

A bit in the destination is set if it is set in **X and also in one or more of Y1, Y2, ...**. This implements "X AND (Y1 OR Y2 OR ...)" logic.

**Use Cases:**

* Required condition with optional alternatives
* Users with a required attribute and at least one optional attribute
* Complex filtering with mandatory and optional criteria

### ONE

A bit in the destination is set if it is set in **exactly one** of the source keys. This finds bits that are unique to a single source.

**Use Cases:**

* Finding unique occurrences
* Identifying exclusive memberships
* Detecting singular conditions across multiple sets

<Note>
  The advanced operations (DIFF, DIFF1, ANDOR, ONE) are available in Redis 8.2.0 and later.
</Note>
