{
  "schema_version": 2,
  "id": "commands/bitcount",
  "title": "BITCOUNT",
  "url": "https://redis.io/docs/latest/commands/bitcount/",
  "summary": "Counts the number of set bits (population counting) in a string.",
  "since": "2.6.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T16:45:12+01:00",
  "page_type": "content",
  "content_hash": "6fca2186964793e61ac664e888896ac9a57047ccb68b1ac81fae80b12967ceae",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Count the number of set bits (population counting) in a string.\n\nBy default all the bytes contained in the string are examined.\nIt is possible to specify the counting operation only in an interval passing the\nadditional arguments _start_ and _end_.\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.\n\nNon-existent keys are treated as empty strings, so the command will return zero.\n\nBy default, the additional arguments _start_ and _end_ specify a byte index.\nWe can use an additional argument `BIT` to specify a bit index.\nSo 0 is the first bit, 1 is the second bit, and so forth.\nFor negative values, -1 is the last bit, -2 is the penultimate, and so forth."
    },
    {
      "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>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open><summary><code>start end</code></summary>\n\nCount set bits only within the range from `start` to `end` (inclusive). By default these are byte offsets; negative values count from the end of the string.\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": "redis> SET mykey \"foobar\"\nOK\nredis> BITCOUNT mykey\n(integer) 26\nredis> BITCOUNT mykey 0 0\n(integer) 4\nredis> BITCOUNT mykey 1 1\n(integer) 6\nredis> BITCOUNT mykey 1 1 BYTE\n(integer) 6\nredis> BITCOUNT mykey 5 30 BIT\n(integer) 17"
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": ""
    },
    {
      "id": "pattern-real-time-metrics-using-bitmaps",
      "title": "Pattern: real-time metrics using bitmaps",
      "role": "content",
      "text": "Bitmaps are a very space-efficient representation of certain kinds of\ninformation.\nOne example is a Web application that needs the history of user visits, so that\nfor instance it is possible to determine what users are good targets of beta\nfeatures.\n\nUsing the [`SETBIT`](https://redis.io/docs/latest/commands/setbit) command this is trivial to accomplish, identifying every day\nwith a small progressive integer.\nFor instance day 0 is the first day the application was put online, day 1 the\nnext day, and so forth.\n\nEvery time a user performs a page view, the application can register that in\nthe current day the user visited the web site using the [`SETBIT`](https://redis.io/docs/latest/commands/setbit) command setting\nthe bit corresponding to the current day.\n\nLater it will be trivial to know the number of single days the user visited the\nweb site simply calling the `BITCOUNT` command against the bitmap.\n\nA similar pattern where user IDs are used instead of days is described\nin the article called \"[Fast easy realtime metrics using Redis\nbitmaps](http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps)\"."
    },
    {
      "id": "performance-considerations",
      "title": "Performance considerations",
      "role": "performance",
      "text": "In the above example of counting days, even after 10 years the application is\nonline we still have just `365*10` bits of data per user, that is just 456 bytes\nper user.\nWith this amount of data `BITCOUNT` is still as fast as any other O(1) Redis\ncommand like [`GET`](https://redis.io/docs/latest/commands/get) or [`INCR`](https://redis.io/docs/latest/commands/incr).\n\nWhen the bitmap is big, there are two alternatives:\n\n* Taking a separated key that is incremented every time the bitmap is modified.\n  This can be very efficient and atomic using a small Redis Lua script.\n* Running the bitmap incrementally using the `BITCOUNT` _start_ and _end_\n  optional parameters, accumulating the results client-side, and optionally\n  caching the result into a key."
    },
    {
      "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\n[Integer reply](../../develop/reference/protocol-spec#integers): the number of bits set to 1.\n\n**RESP3:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the number of bits set to 1."
    }
  ],
  "examples": []
}
