A compact bitset implementation used in Ocarina of Time save files

https://news.ycombinator.com/rss Hits: 4
Summary

OoT Bitset A no‑frills, zero‑overhead flag system inspired by The Legend of Zelda: Ocarina of Time Implemented in C / C++, and Rust Need to pack hundreds (or thousands) of one‑bit flags—“talked to an NPC”, “opened a chest”, etc.—into a save file without wasting bytes? Ocarina of Time solved this by storing flags in an array of uint16_t words. oot_bitset offers the same trick! I learned about this technique from reading the OoT decompilation project source code. See the original code that implements these flags for OoT's save file event information tables. Why use it? Simple, header‑only, zero deps – drop a header (C) or add a tiny dependency (Rust). No heap, no alloc . – drop a header (C) or add a tiny dependency (Rust). No heap, no . Space‑efficient – 1 × u16 word ≙ 16 flags. Scale from 1 to 4096 words (65 536 flags). – 1 × word ≙ 16 flags. Scale from 1 to 4096 words (65 536 flags). Fast – branch‑free bit‑twiddling; compiles to a handful of instructions. – branch‑free bit‑twiddling; compiles to a handful of instructions. Scalable – need 10 flags or 10 000? Just resize the array. – need 10 flags or 10 000? Just resize the array. Intuitive indices for debugging – 0x12 maps to first word, second bit. Why its cool The most novel aspect of OoT bitsets is that the first 4 bits in the 16-bit coordinate IDs index which bit is set. For example, 0xA5 shows that the 5th bit is set in the 10th word of the array of 16-bit integers. This only works in the 16-bit representation! 32 bit words would need 5 bits to index the bit, which wouldn't map cleanly to a nibble for debugging. Bits 15…4 (12 bits) 3…0 (4 bits) Use word index bit index Max 0–4095 words 0–15 bits Because each hex digit is 4 bits, you can read a flag as “word:bit”. Example: 0x1AC → word 26, bit 12. These form a grid, and you can create UIs that visually map these coordinates to a visual editor: Quick start C example #include "oot_bitset.h" #include <stdio.h> enum GameEvents { FLAG_MET_RUTO_FIRST_TIME = 0x00 , // w...

First seen: 2025-07-08 16:31

Last seen: 2025-07-08 19:31