[ANN] BitsFields.jl (one or more fields of bits )

BitsFields is a package for specifying bitfields, collecting bitfield specifiers for multifield designs, and utilizing the bitfields by setting, getting, and altering the values they hold.

This initial release provides Julia with reasonably easy-to-use bitfields. The way forward leans on NamedTuples to move from working with the ith field to using fields by name. The default carrier type into which bitfields are embeded is UInt64. Any of the other unsigned bitstypes may be given when specifying fields. Fields may be formed from one … all bits of carrier type. The bits of a bitfield are contiguous.

an introductory example

We want two bitfields, one that is six bits wide and another that is ten bits wide.
The total span for both bitfields is 6+10 == 16 bits, so a UInt16 will hold them.

using BitsFields

field1span  =  6
field1shift =  0
field2span  = 10
field2shift = field1span

field1 = BitField(UInt16, field1span, field1shift)
field2 = BitField(UInt16, field2span, field2shift)

bitfields = BitFields(bitfield1, bitfield2)

To use the bitfields, provide a referenceable, type-matched and zeroed carrier.

workingbits = Ref(zero(UInt16))

Now we can set the fields and get their values.

field1value = 0x15
field2value = 0x02f6

set!(bitfields[1], field1value, workingbits)
set!(bitfields[2], field2value, workingbits)

get(bitfields[2], workingbits)  # UInt16(0x02f6)

get(bitfields, workingbits)     # [ UInt16(0x15), UInt16(0x02f6) ]
6 Likes

Was coding up my own. Exactly what i needed

I have released the next version of BitsFields.jl. The API is somewhat different: fields are named and this simplifies bitfield access. Indexed access still works.

In addition to the sample code given in the README, I include a worked example, based on the IEEE Floating Point Standard to illustrate the new capabilities.

1 Like