Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bit fields portability

I read here that bit fields are not portable. Does that mean that the code below that defines bit fields (code taken from here) could not compile on certain machines?

If so, then why?

#include <stdio.h>
#include <string.h>

/* define simple structure */
struct
{
  unsigned int widthValidated;
  unsigned int heightValidated;
} status1;

/* define a structure with bit fields */
struct
{
  unsigned int widthValidated : 1;
  unsigned int heightValidated : 1;
} status2;

int main( )
{
   printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
   printf( "Memory size occupied by status2 : %d\n", sizeof(status2));

   return 0;
}
like image 872
teaLeef Avatar asked Aug 17 '14 02:08

teaLeef


People also ask

Are bit fields portable?

Bit fields are portable, in the sense that they are a part of the C language as specified in the standard (C11 section 6.7. 2.1). Any compiler that fails to recognise code that uses bitfields is not standard-compliant.

What are the advantages with bit fields?

Advantages of bit-fields This makes read/write and other operations work as if they were int. In writing routines for encryption/decryption. In accessing the particular system resources. In handling the external file formats ( even the non-standard file formats).

What are bit fields & why it is used?

Bit fields can be used to reduce memory consumption when a program requires a number of integer variables which always will have low values. For example, in many systems storing an integer value requires two bytes (16-bits) of memory; sometimes the values to be stored actually need only one or two bits.

What is a characteristic of bit fields?

In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner. Since structures and unions are user-defined data types in C, the user has an idea of how much memory will they occupy.


1 Answers

Bit fields are portable, in the sense that they are a part of the C language as specified in the standard (C11 section 6.7.2.1). Any compiler that fails to recognise code that uses bitfields is not standard-compliant. There's also nothing really questionable about your example, since all it does is have bitfields present.

What they probably mean is that the fields themselves may be packed unpredictably in location and order (allowed by the standard, previous ref. paragraph 11). This means that a struct with e.g. four bitfields of size 4, 12, 13 and 3 does not necessarily take up 32 bits and they won't necessarily be placed within the struct in that order; the compiler can place them where it likes. This means that the struct cannot be treated as an actual component-wise representation of an underlying binary object.

In contrast, bitmasks applied manually to integers exist exactly where you put them. If you define masks that mask out the first 4 bits, second 12 bits, etc. of an unsigned integer, the "fields" will actually apply to the bits, in order and in position (assuming you know the endianness, anyway). This makes the representation compiler-independent.

i.e. they are portable, but what they do may not necessarily be exactly what a person actually wanting to manipulate individual bits may need.

like image 137
Leushenko Avatar answered Nov 24 '22 13:11

Leushenko