Search code examples
arrayshaskellbitflags

What data structure for an array of bit flags?


I'm porting some imperative code to Haskell. My goal is to analyze an executable, therefore each byte of the text section gets assigned a number of flags, which would all fit in a byte (6 bits to be precise).

In a language like C, I would just allocate an array of bytes, zero them and update them as I go. How would I do this efficiently in Haskell?

In other words: I'm looking for a ByteString with bit-wise access and constant time updates as I disassemble more of the text section.

Edit: Of course, any kind of other data structure would do if it was similarly efficient.


Solution

  • You can use a vector with any data type that's an instance of the Bits typeclass, for example Word64 for 64 bits per element in the vector. Use an unboxed vector if you want the array to be contiguous in memory.