Tuesday 8 November 2011

Are bit fields portable? in C programming

Are bit fields portable?

Bit fields are not portable. Because bit fields cannot span machine words, and because the number of bits
in a machine word is different on different machines, a particular program using bit fields might not even
compile on a particular machine.

Assuming that your program does compile, the order in which bits are assigned to bit fields is not defined.
Therefore, different compilers, or even different versions of the same compiler, could produce code that would not work properly on data generated by compiled older code. Stay away from using bit fields, except
in cases in which the machine can directly address bits in memory and the compiler can generate code to take
advantage of it and the increase in speed to be gained would be essential to the operation of the program.

Cross Reference:

X.1: What is the most efficient way to store flag values?
X.2: What is meant by “bit masking”?

No comments:

Post a Comment