# Working with bit-level data

**URL:** <https://discuss.ocaml.org/t/working-with-bit-level-data/3094>\
**Category:** Ecosystem\
**Created:** [December 24, 2018, 12:19pm UTC](https://discuss.ocaml.org/t/working-with-bit-level-data/3094 "2018-12-24T12:19:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [December 24, 2018, 12:19pm UTC](https://discuss.ocaml.org/t/working-with-bit-level-data/3094/1 "2018-12-24T12:19:09Z")

</div>

I plan to implement bitfields support in [Cstruct](https://github.com/mirage/ocaml-cstruct) library. And I wonder on what syntax is best and probably an API, maybe there are already some good examples of OCaml libraries that work with bit-level data? If you ask - why not to just aggregate bits into bytes, there are two problems:

1. If you don’t align bitfields, all members can be shifted across byte boundary to be read directly
2. Changing the way to read/write in case of different endianess

See [https://github.com/mirage/ocaml-cstruct/issues/205](https://github.com/mirage/ocaml-cstruct/issues/205) for more information.

---

<div class="post-metadata">

**Author:** ![anuragsoni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/anuragsoni/32/1107_2.png) [@anuragsoni](https://discuss.ocaml.org/u/anuragsoni)\
**Post date:** [December 24, 2018, 12:56pm UTC](https://discuss.ocaml.org/t/working-with-bit-level-data/3094/2 "2018-12-24T12:56:14Z")

</div>

Is [https://github.com/xguerin/bitstring](https://github.com/xguerin/bitstring) similar to what you are looking for?

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [December 24, 2018, 1:20pm UTC](https://discuss.ocaml.org/t/working-with-bit-level-data/3094/3 "2018-12-24T13:20:55Z")

</div>

Yes, it is similar, thanks.

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [January 10, 2019, 5:22am UTC](https://discuss.ocaml.org/t/working-with-bit-level-data/3094/4 "2019-01-10T05:22:34Z")

</div>

Note, that I sent an initial draft of bitfields implementation in Cstruct library (for now only reading is supported) here [https://github.com/mirage/ocaml-cstruct/pull/215](https://github.com/mirage/ocaml-cstruct/pull/215)

You are welcome to check out, and feedback/complaints/patches are welcome.

It adds a syntax like this:

```
[%%cstruct
type some_struct = {
       field1: uint8_t;
       field2: uint32_t [@bits 15];
       field3: uint32_t [@bits 4];
       field4: uint16_t;
} [@@big_endian]]

```

which is an equivalent to C

```
struct some_struct {
       uint8_t field1;
       uint32_t field2:15;
       uint32_t field3:4;
       uint16_t field4;
} __attribute__ ((packed));
```
