# BITOP

> Combine strings bit by bit with AND, OR, XOR, NOT, DIFF, DIFF1, ANDOR or ONE and store the result.

URL: https://docs.hotkv.com/commands/bitop/

```
BITOP AND destkey key [key ...]
BITOP OR destkey key [key ...]
BITOP XOR destkey key [key ...]
BITOP NOT destkey key
BITOP DIFF destkey key key [key ...]
BITOP DIFF1 destkey key key [key ...]
BITOP ANDOR destkey key key [key ...]
BITOP ONE destkey key [key ...]
```

- Group: Bitmap
- Complexity: O(N) where N is the length of the longest input string
- HotKV Cloud: all plans
- On-Premise: no license needed

`BITOP` combines the strings at the source keys bit by bit and stores the
result at `destkey`, replacing whatever was there. Shorter inputs are treated
as if padded with zero bytes to the length of the longest one, and a missing
source counts as an empty string. `NOT` takes exactly one source and inverts
every bit. If the result is empty (all sources missing or empty), `destkey` is
deleted.

In a cluster, all keys must hash to the same slot.

## Options

| Operation | Result bit |
| --- | --- |
| `AND` | 1 when the bit is 1 in every source |
| `OR` | 1 when the bit is 1 in at least one source |
| `XOR` | 1 when the bit is 1 in an odd number of sources |
| `NOT` | The inverse of the single source's bit |
| `DIFF` | 1 when the bit is 1 in the first source and 0 in every other source |
| `DIFF1` | 1 when the bit is 1 in at least one of the other sources and 0 in the first |
| `ANDOR` | 1 when the bit is 1 in the first source and in at least one of the other sources |
| `ONE` | 1 when the bit is 1 in exactly one source |

`DIFF`, `DIFF1` and `ANDOR` need at least two source keys. `ONE` accepts one or more.
The operation name is not case-sensitive.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Always | Integer: the length in bytes of the stored result | Integer |

Errors:

- `ERR BITOP NOT must be called with a single source key.`
- `ERR BITOP DIFF must be called with at least two source keys.` (and the same for `DIFF1` and `ANDOR`).
- `ERR syntax error` for an unknown operation.
- `WRONGTYPE` when a source key holds a value that is not a string.

## Examples

```
127.0.0.1:6379> SET a "\xf0"
OK
127.0.0.1:6379> SET b "\x0f\x01"
OK
127.0.0.1:6379> BITOP OR both a b
(integer) 2
127.0.0.1:6379> GET both
"\xff\x01"
127.0.0.1:6379> BITOP AND common a b
(integer) 2
127.0.0.1:6379> GET common
"\x00\x00"
127.0.0.1:6379> BITOP NOT inv a
(integer) 1
127.0.0.1:6379> GET inv
"\x0f"
```

The four operations added in Redis 8.2 compare the first source with the
others. With `x` = `11110000`, `y` = `00111100` and `z` = `00001111`:

```
127.0.0.1:6379> SET x "\xf0"
OK
127.0.0.1:6379> SET y "\x3c"
OK
127.0.0.1:6379> SET z "\x0f"
OK
127.0.0.1:6379> BITOP DIFF only_x x y z
(integer) 1
127.0.0.1:6379> GET only_x
"\xc0"
127.0.0.1:6379> BITOP DIFF1 not_x x y z
(integer) 1
127.0.0.1:6379> GET not_x
"\x0f"
127.0.0.1:6379> BITOP ANDOR x_and_other x y z
(integer) 1
127.0.0.1:6379> GET x_and_other
"0"
127.0.0.1:6379> BITOP ONE exactly_one x y z
(integer) 1
127.0.0.1:6379> GET exactly_one
"\xc3"
```

## Compatibility

Same as Redis 8.2, including the `DIFF`, `DIFF1`, `ANDOR` and `ONE`
operations.

## See also

[BITCOUNT](https://docs.hotkv.com/commands/bitcount/index.md), [SETBIT](https://docs.hotkv.com/commands/setbit/index.md),
[BITFIELD](https://docs.hotkv.com/commands/bitfield/index.md)