{
  "schema_version": 2,
  "id": "commands/bitop",
  "title": "BITOP",
  "url": "https://redis.io/docs/latest/commands/bitop/",
  "summary": "Performs bitwise operations on multiple strings, and stores the result.",
  "since": "2.6.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T16:45:12+01:00",
  "page_type": "content",
  "content_hash": "ad47b01ea1ac76b64e1eac8190c32bf4a92f05ad3bea53035451505268c1cce5",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "> [!NOTE]\n> This command's behavior varies in clustered Redis environments. See the [multi-key operations](https://redis.io/docs/latest/develop/using-commands/multi-key-operations) page for more information.\n\n\nPerform a bitwise operation between multiple keys (containing string values) and\nstore the result in the destination key.\n\nThe `BITOP` command supports eight bitwise operations: `AND`, `OR`, `XOR`,\n`NOT`, `DIFF`, `DIFF1`, `ANDOR`, and `ONE`. The valid forms to call the command are:\n\n* `BITOP AND destkey srckey1 srckey2 srckey3 ... srckeyN`\n\n    A bit in `destkey` is set only if it is set in all source bitmaps.\n* `BITOP OR  destkey srckey1 srckey2 srckey3 ... srckeyN`\n\n    A bit in `destkey` is set only if it is set in at least one source bitmap.\n* `BITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyN`\n\n    Mostly used with two source bitmaps, a bit in `destkey` is set only if its value differs between the two source bitmaps.\n* `BITOP NOT destkey srckey`\n\n    `NOT` is a unary operator and only supports a single source bitmap; set the bit to the inverse of its value in the source bitmap.\n* `BITOP DIFF destkey X [Y1 Y2 ...]` <sup>[1](#list-note-1)</sup>\n\n    A bit in `destkey` is set if it is set in `X`, but not in any of `Y1, Y2, ...` .\n* `BITOP DIFF1 destkey X [Y1 Y2 ...]` <sup>[1](#list-note-1)</sup>\n\n    A bit in `destkey` is set if it is set in one or more of `Y1, Y2, ...`, but not in `X`.\n* `BITOP ANDOR destkey X [Y1 Y2 ...]` <sup>[1](#list-note-1)</sup>\n\n    A bit in `destkey` is set if it is set in `X` and also in one or more of `Y1, Y2, ...`.\n* `BITOP ONE destkey X1 [X2 X3 ...]` <sup>[1](#list-note-1)</sup>\n\n    A bit in `destkey` is set if it is set in exactly one of `X1, X2, ...`.\n\nThe result of each operation is always stored at `destkey`.\n\n1. <a name=\"list-note-1\"></a> Added in Redis 8.2."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>operation</code></summary>\n\nThe bitwise operation to perform: `AND`, `OR`, `XOR`, `NOT`, `DIFF`, `DIFF1`, `ANDOR`, or `ONE`. `NOT` accepts exactly one source key.\n\n</details>\n\n<details open><summary><code>destkey</code></summary>\n\nThe key to store the result in.\n\n</details>\n\n<details open><summary><code>key [key ...]</code></summary>\n\nOne or more source keys.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "1. Basic usage example using the `AND` operator:\n\n\nredis> BITFIELD key1 SET i8 #0 255\n1) (integer) 0\nredis> BITFIELD key2 SET i8 #0 85\n1) (integer) 0\nredis> BITOP AND dest key1 key2\n(integer) 1\nredis> BITFIELD dest GET i8 #0\n1) (integer) 85\n\n\n2. Suppose you want to expose people to a book-related ad. The target audience is people who love to read books and are interested in fantasy, adventure, or science fiction. Assume you have the following bitmaps:\n\n* `LRB` - people who love to read books.\n* `B:F` - people interested in fantasy.\n* `B:A` - people interested in adventure.\n* `B:SF` - people interested in science fiction.\n\nTo create a bitmap representing the target audience, use the following command:\n\n[code example]"
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": ""
    },
    {
      "id": "handling-of-strings-with-different-lengths",
      "title": "Handling of strings with different lengths",
      "role": "content",
      "text": "When an operation is performed between strings having different lengths, all the\nstrings shorter than the longest string in the set are treated as if they were\nzero-padded up to the length of the longest string.\n\nThe same holds true for non-existent keys, that are considered as a stream of\nzero bytes up to the length of the longest string."
    },
    {
      "id": "pattern-real-time-metrics-using-bitmaps",
      "title": "Pattern: real time metrics using bitmaps",
      "role": "content",
      "text": "`BITOP` is a good complement to the pattern documented in the [`BITCOUNT`](https://redis.io/docs/latest/commands/bitcount) command\ndocumentation.\nDifferent bitmaps can be combined in order to obtain a target bitmap where\nthe population counting operation is performed.\n\nSee the article called \"[Fast easy realtime metrics using Redis\nbitmaps](http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps)\" for an interesting use cases."
    },
    {
      "id": "performance-considerations",
      "title": "Performance considerations",
      "role": "performance",
      "text": "`BITOP` is a potentially slow command as it runs in O(N) time.\nCare should be taken when running it against long input strings.\n\nFor real-time metrics and statistics involving large inputs a good approach is\nto use a replica (with replica-read-only option enabled) where the bit-wise\noperations are performed to avoid blocking the master instance."
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the size of the string stored in the destination key is equal to the size of the longest input string.\n\n**RESP3:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the size of the string stored in the destination key is equal to the size of the longest input string."
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "plaintext",
      "code": "BITOP ANDOR TA LRB B:F B:A B:SF",
      "section_id": "examples"
    }
  ]
}
