Skip to main content
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

operation
AND | OR | XOR | NOT | DIFF | DIFF1 | ANDOR | ONE | and | or | xor | not | diff | diff1 | andor | one
required
Specifies the type of bitwise operation to perform (case-insensitive):Standard Operations:
  • AND or and: Bitwise AND
  • OR or or: Bitwise OR
  • XOR or xor: Bitwise XOR
  • NOT or not: Bitwise NOT
Advanced Operations (Redis 8.2+):
  • DIFF or diff: A bit is set only if it’s set in all source bitmaps
  • DIFF1 or diff1: A bit is set if it’s set in the first key but not in any of the other keys
  • ANDOR or andor: A bit is set if it’s set in X and also in one or more of Y1, Y2, …
  • ONE or one: A bit is set if it’s set in exactly one source key
destkey
str
required
The key to store the result of the operation in.
keys
*List[str]
required
One or more keys to perform the operation on.

Response

The size of the string stored in the destination key.
# AND operation
redis.setbit("key1", 0, 1)
redis.setbit("key2", 0, 0)
redis.setbit("key2", 1, 1)

assert redis.bitop("AND", "dest", "key1", "key2") == 1

# OR operation
redis.bitop("OR", "dest", "key1", "key2")

# XOR operation
redis.bitop("XOR", "dest", "key1", "key2")

# NOT operation (only accepts one source key)
redis.bitop("NOT", "dest", "key1")

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
The advanced operations (DIFF, DIFF1, ANDOR, ONE) are available in Redis 8.2.0 and later.