yeslogic / fathom

🚧 (Alpha stage software) A declarative data definition language for formally specifying binary data formats. 🚧

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bit flags

wezm opened this issue · comments

For parsing some formats it's necessary to consult the bits of a value. For example in hvif there are flags in gradients that indicate the size of the colours in the steps of the gradient.

The advanced way to handle this might be with bit-sized types or erlang-style bit patterns.

kaitai has done some interesting prior work on the former: https://doc.kaitai.io/user_guide.html#_bit_sized_integers as has Zig: https://andrewkelley.me/post/a-better-way-to-implement-bit-fields.html

A simpler option could be bitwise primitive ops.

In cases where you just want flag bits you could have bitfield syntax that just desugars into bitwise ops. Something like flags: u8 bits { foo, bar, etc. } where flags.foo is equivalent to flags & mask.

Full disclosure: I copied a bunch of this from chat logs, so credit to the original authors for the content.