For the complete documentation index, see llms.txt. This page is also available as Markdown.

Manual bit packing

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

manual-bit-packing.gpc
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;
}

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.

Last updated