# 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:** 1\
**Showing post:** 4

<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));
```

---

_[View the full topic](https://discuss.ocaml.org/t/working-with-bit-level-data/3094)._
