16.4 Raw Output Internals

mpz_out_raw uses the following format.

+------+------------------------+
| size |       data bytes       |
+------+------------------------+

The size is 4 bytes written most significant byte first, being the number of subsequent data bytes, or the two’s complement negative of that when a negative integer is represented. The data bytes are the absolute value of the integer, written most significant byte first.

The most significant data byte is always non-zero, so the output is the same on all systems, irrespective of limb size.

In GMP 1, leading zero bytes were written to pad the data bytes to a multiple of the limb size. mpz_inp_raw will still accept this, for compatibility.

The use of “big endian” for both the size and data fields is deliberate, it makes the data easy to read in a hex dump of a file. Unfortunately it also means that the limb data must be reversed when reading or writing, so neither a big endian nor little endian system can just read and write _mp_d.