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
- Arity
- -4 (at least 4 words, counting the command name)
- Flags
- write, slow
- Keys
- first 2, last final argument, step 1
- 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
Section titled “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
Section titled “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 forDIFF1andANDOR).ERR syntax errorfor an unknown operation.WRONGTYPEwhen a source key holds a value that is not a string.
Examples
Section titled “Examples”127.0.0.1:6379> SET a "\xf0"OK127.0.0.1:6379> SET b "\x0f\x01"OK127.0.0.1:6379> BITOP OR both a b(integer) 2127.0.0.1:6379> GET both"\xff\x01"127.0.0.1:6379> BITOP AND common a b(integer) 2127.0.0.1:6379> GET common"\x00\x00"127.0.0.1:6379> BITOP NOT inv a(integer) 1127.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"OK127.0.0.1:6379> SET y "\x3c"OK127.0.0.1:6379> SET z "\x0f"OK127.0.0.1:6379> BITOP DIFF only_x x y z(integer) 1127.0.0.1:6379> GET only_x"\xc0"127.0.0.1:6379> BITOP DIFF1 not_x x y z(integer) 1127.0.0.1:6379> GET not_x"\x0f"127.0.0.1:6379> BITOP ANDOR x_and_other x y z(integer) 1127.0.0.1:6379> GET x_and_other"0"127.0.0.1:6379> BITOP ONE exactly_one x y z(integer) 1127.0.0.1:6379> GET exactly_one"\xc3"Compatibility
Section titled “Compatibility”Same as Redis 8.2, including the DIFF, DIFF1, ANDOR and ONE
operations.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.