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

**URL:** https://discourse.julialang.org/t/ann-bitsfields-jl-one-or-more-fields-of-bits/16933
**Category:** Package Announcements
**Tags:** package, announcement
**Created:** [October 29, 2018, 8:25pm UTC](https://discourse.julialang.org/t/ann-bitsfields-jl-one-or-more-fields-of-bits/16933 "2018-10-29T20:25:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [October 29, 2018, 8:25pm UTC](https://discourse.julialang.org/t/ann-bitsfields-jl-one-or-more-fields-of-bits/16933/1 "2018-10-29T20:25:49Z")

</div>

[BitsFields](https://github.com/JeffreySarnoff/BitsFields.jl/) 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.

```julia-auto
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.

```julia-auto
workingbits = Ref(zero(UInt16))

```

Now we can set the fields and get their values.

```julia-auto
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)]

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 29, 2018, 9:07pm UTC](https://discourse.julialang.org/t/ann-bitsfields-jl-one-or-more-fields-of-bits/16933/2 "2018-10-29T21:07:16Z")

</div>

Was coding up my own. Exactly what i needed

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [December 11, 2018, 7:02pm UTC](https://discourse.julialang.org/t/ann-bitsfields-jl-one-or-more-fields-of-bits/16933/3 "2018-12-11T19:02:22Z")

</div>

I have released the next version of [BitsFields.jl](https://github.com/JeffreySarnoff/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](https://github.com/JeffreySarnoff/BitsFields.jl/blob/master/README.md), I include a worked example, based on the [IEEE Floating Point Standard](https://github.com/JeffreySarnoff/BitsFields.jl/blob/master/example/ieeefields.jl) to illustrate the new capabilities.
