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