BITRANGEINTARRAY

BITRANGEINTARRAY key start end [BYTE | BIT] [LIMIT count] [ZEROS]
Available since:
Redis Open Source 8.12.0
Time complexity:
O(N + M) where N is the number of bytes in the range for a string, or the number of bitmap containers in the range (and of runs of set bits in it, with ZEROS), and M is the number of returned integers. With LIMIT, the scan stops after count matches, or after completing their last byte in BYTE units.
ACL categories:
@read, @bitmap, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility

Returns set-bit offsets within a bitmap range in ascending order. Use ZEROS to return clear-bit offsets instead. The command reads strings and native bitmaps regardless of bitmap-default-roaring, without changing the stored value.

Offsets use the same numbering as GETBIT: bit 0 is the most significant bit of the first byte. Returned offsets are always bit offsets, including when the range uses BYTE units.

Required arguments

key

The key that holds a string or native bitmap.

start

The first byte or bit offset in the range, inclusive. The default unit is BYTE. Negative indexes count from the end of the value, as in BITCOUNT.

end

The last byte or bit offset in the range, inclusive. Negative indexes count from the end: -1 selects the final byte, or the final bit with BIT. Use 0 -1 to cover the whole value.

Optional arguments

BYTE | BIT

The unit for start and end. BYTE is the default. The range selects positions in the stored value, not ranks in the returned array.

LIMIT count

Return the lowest matching offsets in the range. With BIT, the reply contains at most count offsets. With BYTE, the command also returns the remaining matching offsets in the byte containing the countth match. The reply can therefore contain up to count + 7 offsets.

LIMIT 0 returns an empty array. A negative count is an error. Without LIMIT, the command returns every matching offset in the range.

ZEROS

Return clear-bit offsets instead of set-bit offsets within the range.

The unit, LIMIT, and ZEROS options can appear in any order after the required arguments. If an option is repeated, its last value applies.

Examples

redis> CONFIG SET bitmap-default-roaring yes
OK
redis> SETBIT members 1 1
(integer) 0
redis> SETBIT members 7 1
(integer) 0
redis> SETBIT members 9 1
(integer) 0
redis> SETBIT members 30 1
(integer) 0
redis> BITRANGEINTARRAY members 0 -1
1) (integer) 1
2) (integer) 7
3) (integer) 9
4) (integer) 30
redis> BITRANGEINTARRAY members 1 -1
1) (integer) 9
2) (integer) 30
redis> BITRANGEINTARRAY members 5 10 BIT
1) (integer) 7
2) (integer) 9
redis> BITRANGEINTARRAY members -1 -1
1) (integer) 30
redis> BITRANGEINTARRAY members -1 -1 BIT ZEROS
1) (integer) 31
redis> BITRANGEINTARRAY members 28 100 BIT ZEROS
1) (integer) 28
2) (integer) 29
3) (integer) 31
redis> BITRANGEINTARRAY members 0 -1 BIT LIMIT 2
1) (integer) 1
2) (integer) 7
redis> BITRANGEINTARRAY members 8 -1 BIT LIMIT 2
1) (integer) 9
2) (integer) 30
redis> BITRANGEINTARRAY members 0 -1 LIMIT 1
1) (integer) 1
2) (integer) 7
redis> BITRANGEINTARRAY members 1 -1 LIMIT 1
1) (integer) 9
redis> BITRANGEINTARRAY members 0 -1 LIMIT 0
(empty array)
redis> CONFIG SET bitmap-default-roaring no
OK
redis> SET rawbits a
OK
redis> BITRANGEINTARRAY rawbits 0 -1
1) (integer) 1
2) (integer) 2
3) (integer) 7
redis> BITRANGEINTARRAY rawbits 0 -1 ZEROS
1) (integer) 0
2) (integer) 3
3) (integer) 4
4) (integer) 5
5) (integer) 6
redis> GET rawbits
"a"
redis> TYPE rawbits
string

Details

Ranges and clear bits

start and end follow BITCOUNT range semantics. They accept signed 64-bit integers, and negative indexes count from the logical end of the value in the selected unit. Bounds are clamped to the value. A reversed range or a start past the end returns an empty array.

The range never extends beyond the value's logical length, including when using ZEROS. An all-ones value therefore returns an empty array with ZEROS. This differs from BITPOS key 0, which can report the first bit past the end. Missing keys and empty values have no matching bits.

Without LIMIT, the number of set-bit offsets equals BITCOUNT over the same range. With ZEROS, it equals the number of bits in the normalized range minus that count.

Key behavior

Arguments are validated before looking up the key. With valid arguments, a missing key returns an empty array. Strings and native bitmaps work under either bitmap-default-roaring setting; other types return WRONGTYPE.

Strings are read directly without conversion. Reads leave the stored bytes, type, and expiration unchanged.

Pagination and performance

Use LIMIT to page through matching offsets. Keep the same end, unit, and ZEROS setting for each page. Choose the next start from the last returned bit offset:

Unit Maximum page size Next start
BIT count Last offset + 1
BYTE count + 7 floor(last offset / 8) + 1

Use a positive count. A page with fewer than count results means the range is exhausted. A full page might require one final request that returns an empty array. Separate calls do not share a snapshot: writes between requests can change the remaining matches. The continuation rules avoid repeating or skipping offsets for an unchanged value.

Native bitmaps seek to the start offset and enumerate matching bits. Strings scan the requested range in place. LIMIT stops the scan once the page is complete; a range with few matches can still require scanning the whole range. No temporary bitmap or second result array is allocated.

Response size and serialization cost grow with the number of returned offsets. There is no fixed result-count cap. Configured client output-buffer limits still apply; exceeding a limit disconnects the client and stops enumeration.

Returned offsets retain the bitmap's supported range, including values above 4294967295 for native bitmaps created with a sufficiently large proto-max-bulk-len. Clients must preserve the precision of returned integers.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
❌ Standard
❌ Active-Active
❌ Standard
❌ Active-Active

Return information

Array reply: Offsets of the set bits in the range, or of the clear bits with ZEROS, in ascending order. The range is interpreted as in BITCOUNT: negative indexes count from the end, and the unit defaults to BYTE. Unlike BITPOS, the range never extends past the end of the bitmap, so no clear bits are returned beyond it. With LIMIT, the lowest count offsets in the range are returned. In BYTE units the reply also completes the byte holding the last of them, so it can hold up to 7 more offsets and the next page can start at the following byte. Missing keys and empty ranges return an empty array.
RATE THIS PAGE
Back to top ↑