> For the complete documentation index, see [llms.txt](https://guide.cronuszen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide.cronuszen.com/gpcscripting/gpc-script-guide/data-and-persistence/manual-bit-packing.md).

# Manual bit packing

Use source bitwise operators to combine small fields in one 32-bit SPVAR:

{% code title="manual-bit-packing.gpc" %}

```cpp
define MODE_SHIFT = 0;
define MODE_MASK   = 0x00000003; // 2 bits
define ENABLE_SHIFT = 2;
define ENABLE_MASK  = 0x00000004; // 1 bit

int packed;
int mode;
int enabled;

function pack_settings() {
    packed = 0;
    packed = packed | ((mode << MODE_SHIFT) & MODE_MASK);
    packed = packed | ((enabled << ENABLE_SHIFT) & ENABLE_MASK);
    return packed;
}

function unpack_settings(value) {
    mode = (value & MODE_MASK) >> MODE_SHIFT;
    enabled = (value & ENABLE_MASK) >> ENABLE_SHIFT;
    return 0;
}
```

{% endcode %}

Packing rules:

* Document bit offset, width, signedness, min/max, and owner for every field.
* Clamp before packing and validate after unpacking.
* Save and load in the same order.
* A field may span storage boundaries in a cursor-based scheme.
* Inserting or resizing a field changes everything after it; bump the schema version.
* Parenthesize bitwise operations and comparisons.

The callable bit helpers `set_bit`, `clear_bit`, `test_bit`, `set_bits`, and `get_bits` are documented in the additional built-in reference. Use them when they make a packed schema clearer than manual shifts and masks. Both approaches are valid; keep the save and load layouts identical.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://guide.cronuszen.com/gpcscripting/gpc-script-guide/data-and-persistence/manual-bit-packing.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
