Bit fields are not portable in the same sense that integer fields are not portable. You can use integers to write a portable program, but you cannot expect to send the binary representation of int as it is to the remote machine and expect it to interpret the data correctly.
This is due to the fact that 1. the word lengths of the processors are different, and because of this, the sizes of integer types are different (the length of 1.1 bytes can also differ, but these days it is rarely found outside of embedded systems). And because 2. the byte order of bytes is different for different processors.
These problems are easily overcome. A custom sequence number can easily be converted to a consistent sequence number (a large sequence number is the de facto standard for networking), and the size can be checked at compile time, and integer types of a fixed length are currently available. Thus, integers can be used for network communications if these details are taken care of.
Bit fields are based on regular integer types, so they have the same problems with byte order and integer sizes. But they have even more implementation of this behavior.
All about the actual details of placing bit fields in a class object
- For example, on some platforms, bit fields do not intersect bytes, on others -
- In addition, on some platforms, bit fields are packed from left to right, on others from right to left
Be it char, short, int, long, or long long bit fields that are not explicitly signed or not signed, signed or not signed.
Unlike byte ordering, it is not trivial to convert “everything about the actual distribution details” to canonical form.
In addition, although the byte order depends on the processor architecture, the details of the bit fields depend on the compiler developer. Thus, bit fields are not portable for communication even between separate processes on the same computer, unless we can guarantee that they were compiled using one (or binary compatible) compiler.
TL DR bit fields are not a portable way of communication between computers. Integers are also not, but their intolerance is easy to circumvent.
source share