Making the most of bit arrays in Gleam
a day ago
- #programming
- #bit-arrays
- #Gleam
- Bit arrays in Gleam are delimited by << and >> and consist of segments with optional encoding specifications.
- Default segment type is Int, but Float, String, UtfCodepoint, and BitArray types are also supported with specific options.
- Segment size can be customized using the size option, with shorthand syntax available for common cases.
- Endianness can be specified with little, big, or native options, affecting how segments are encoded.
- Pattern matching on bit arrays allows extracting data, with support for matching literals or assigning to variables.
- Strings and UTF codepoints have fixed sizes based on their encoding, and arbitrary length strings cannot be matched directly.
- Signedness for integers can be specified with the signed option, interpreting numbers using two's complement.
- JavaScript support for bit arrays is incomplete, missing native endianness and UTF codepoint pattern matching.
- Example provided for encoding and decoding Minecraft's NBT format using bit arrays.