BITPOS

Redis Developer Course Redis Cloud Service Redis-Gate Server

Return the position of the first bit set to 1 or 0 in a string

The position is returned, thinking of the string as an array of bits from left to right, where the first byte's most significant bit is at position 0, the second byte's most significant bit is at position 8, and so forth.
By default, all the bytes contained in the string are examined.   It is possible to look for bits only in a specified interval passing the additional arguments start and end (it is possible to just pass start, the operation will assume that the end is the last byte of the string).   The range is interpreted as a range of bytes and not a range of bits, so start=0 and end=2 means to look at the first three bytes.   Note that bit positions are returned always as absolute values starting from bit zero even when start and end are used to specify a range.
Like for the GETRANGE command start and end can contain negative values in order to index bytes starting from the end of the string, where -1 is the last byte, -2 is the penultimate, and so forth.   Non-existent keys are treated as empty strings.
Usage is BITPOS key bit [start [end]].

Return value

Integer reply: The command returns the position of the first bit set to 1 or 0 according to the request.

Example

command>set key ABC
result>OK
command>bitpos key 0
result>0
command>bitpos key 1
result>1
command>bitpos key 1 2
result>17

Animation



Syntax

BITPOS key bit [start [end]]

  • Available since 2.8.7.
  • Time complexity(Logical processing time): O(N)
Related Commands SETBIT, GETBIT, BITOP, BITPCOUNT
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

To practice the command, click here to run Redis Web Client


<< BITPCOUNT BITPOS LISTS Introduction >>

Click for a question or to see comments.  Comments :    Viewed :

Email 답글이 올라오면 이메일로 알려드리겠습니다.