This is purely a theoretical problem, nothing I have really found myself in, but it has piqued my curiosity and wanted to see if anyone has a better solution for it:

> 
> How do you portably guarantee that an specific file format / network
>  protocol or whatever conforms to a specific bit pattern.
> 
> 
> 

Say we have a file format that uses a 64 bit header struct immediately followed by a variable length array of 32 bit structures:

```
Header:  magic : 32 bit
         count : 32 bit

Field :  id   : 16 bit
         data : 16 bit

```

My first instinct would be to write something like:

```
struct Field
{
    uint16_t id   ;
    uint16_t data ;
};

```

Except that our compiler may decide that padding is advisable and we end up with a 64 bit structure. So our next bet is:

```
using Field = uint16_t[2];

```

and work on that.

That is, unless someone has carefully read the standard and noticed that uint16\_t is optional. At this point our next best friend is uint\_least16\_t, which is guaranteed to be **at least** 16 bits long, but for all we know could be 20 bits long in a 10 bit / char processor.

At this point, the only real solution I can come up with is some sort of bit stream, capable of reading and writing specific amounts of bits, and adaptable by std::numeric\_limits.

So, is there someone out there who has **very** carefully read the standard and found the point I'm missing? Or it is this the only real way of having a portable guarantee. 

Notes:
 - I've just realized that endianness would probably add another layer of complexity.
 - I'm using the current working draft of the ISO standard (N3797).