{
  "schema_version": 2,
  "id": "commands/bitpos",
  "title": "BITPOS",
  "url": "https://redis.io/docs/latest/commands/bitpos/",
  "summary": "Finds the first set (1) or clear (0) bit in a string.",
  "since": "2.8.7",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-10-16T18:59:00+05:30",
  "page_type": "content",
  "content_hash": "114049136f02438aeba6224675d381d998e06d5bc4f2f4ca2f3c8273b912d998",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Return the position of the first bit set to 1 or 0 in a string.\n\nThe position is returned, thinking of the string as an array of bits from left to\nright, where the first byte's most significant bit is at position 0, the second\nbyte's most significant bit is at position 8, and so forth.\n\nThe same bit position convention is followed by [`GETBIT`](https://redis.io/docs/latest/commands/getbit) and [`SETBIT`](https://redis.io/docs/latest/commands/setbit).\n\nBy default, all the bytes contained in the string are examined.\nIt 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. However there are semantic differences as explained later).\nBy default, 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.\n\nYou can use the optional `BIT` modifier to specify that the range should be interpreted as a range of bits.\nSo `start=0` and `end=2` means to look at the first three bits.\n\nNote that bit positions are returned always as absolute values starting from bit zero even when _start_ and _end_ are used to specify a range.\n\nLike for the [`GETRANGE`](https://redis.io/docs/latest/commands/getrange) command start and end can contain negative values in\norder to index bytes starting from the end of the string, where -1 is the last\nbyte, -2 is the penultimate, and so forth. When `BIT` is specified, -1 is the last\nbit, -2 is the penultimate, and so forth.\n\nIf the key does not exist, it is treated as an infinite array of zero bits: searching for a clear bit (0) returns 0, while searching for a set bit (1) returns -1. In contrast, an existing empty string (a key holding \"\") contains no bits at all, so searching for either 0 or 1 always returns -1."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key that holds the string.\n\n</details>\n\n<details open><summary><code>bit</code></summary>\n\nThe bit value to search for: `0` or `1`.\n\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open><summary><code>start</code></summary>\n\nThe offset to start searching from. By default a byte offset; negative values count from the end of the string.\n\n</details>\n\n<details open><summary><code>end</code></summary>\n\nThe offset to stop searching at, inclusive.\n\n</details>\n\n<details open><summary><code>BYTE | BIT</code></summary>\n\nInterpret `start` and `end` as byte offsets (`BYTE`, the default) or bit offsets (`BIT`).\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "[code example]"
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Integer reply](../../develop/reference/protocol-spec#integers): the position of the first bit set to 1 or 0 according to the request\n* [Integer reply](../../develop/reference/protocol-spec#integers): `-1`. In case the `bit` argument is 1 and the string is empty or composed of just zero bytes\nIf we look for set bits (the bit argument is 1) and the string is empty or composed of just zero bytes, -1 is returned.\nIf we look for clear bits (the bit argument is 0) and the string only contains bits set to 1, the function returns the first bit not part of the string on the right. So if the string is three bytes set to the value `0xff` the command `BITPOS key 0` will return 24, since up to bit 23 all the bits are 1.\nThe function considers the right of the string as padded with zeros if you look for clear bits and specify no range or the _start_ argument **only**.\nHowever, this behavior changes if you are looking for clear bits and specify a range with both _start_ and _end_.\nIf a clear bit isn't found in the specified range, the function returns -1 as the user specified a clear range and there are no 0 bits in that range.\n\n**RESP3:**\n\nOne of the following:\n* [Integer reply](../../develop/reference/protocol-spec#integers): the position of the first bit set to 1 or 0 according to the request\n* [Integer reply](../../develop/reference/protocol-spec#integers): `-1`. In case the `bit` argument is 1 and the string is empty or composed of just zero bytes\nIf we look for set bits (the bit argument is 1) and the string is empty or composed of just zero bytes, -1 is returned.\nIf we look for clear bits (the bit argument is 0) and the string only contains bits set to 1, the function returns the first bit not part of the string on the right. So if the string is three bytes set to the value `0xff` the command `BITPOS key 0` will return 24, since up to bit 23 all the bits are 1.\nThe function considers the right of the string as padded with zeros if you look for clear bits and specify no range or the _start_ argument **only**.\nHowever, this behavior changes if you are looking for clear bits and specify a range with both _start_ and _end_.\nIf a clear bit isn't found in the specified range, the function returns -1 as the user specified a clear range and there are no 0 bits in that range."
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "redis",
      "code": "redis> SET mykey \"\\xff\\xf0\\x00\"\nOK\nredis> BITPOS mykey 0\n(integer) 12\nredis> SET mykey \"\\x00\\xff\\xf0\"\nOK\nredis> BITPOS mykey 1 0\n(integer) 8\nredis> BITPOS mykey 1 2\n(integer) 16\nredis> BITPOS mykey 1 2 -1 BYTE\n(integer) 16\nredis> BITPOS mykey 1 7 15 BIT\n(integer) 8\nredis> set mykey \"\\x00\\x00\\x00\"\nOK\nredis> BITPOS mykey 1\n(integer) -1\nredis> BITPOS mykey 1 7 -3 BIT\n(integer) -1",
      "section_id": "examples"
    }
  ]
}
