J
Jason Curl
Hello C Group,
From what I remember, it is implementation dependent the way that a
compiler may order bitfields in a struct. In the example program I give,
Solaris Sparc gives one result, Intel x86 gives another result (for the
GCC compiler).
Is there a portable way to test how this information is stored? In
particular, will the result of the program below always result in c
being 0x01 or 0x80? Does the compiler allow for padding so that the
result may also be 0x00?
For example:
#include <stdio.h>
struct bitfield {
int b7:1;
int b6:1;
int b5:1;
int b4:1;
int b3:1;
int b2:1;
int b1:1;
int b0:1;
};
union highlow {
struct bitfield bf;
unsigned char c;
};
int main(void)
{
union highlow h;
h.c = 0;
h.bf.b0 = 1;
if (h.c == 0x80) {
printf("LSB machine\n");
} else {
printf("MSB machine\n");
}
printf("c = %02x\n", h.c);
}
From what I remember, it is implementation dependent the way that a
compiler may order bitfields in a struct. In the example program I give,
Solaris Sparc gives one result, Intel x86 gives another result (for the
GCC compiler).
Is there a portable way to test how this information is stored? In
particular, will the result of the program below always result in c
being 0x01 or 0x80? Does the compiler allow for padding so that the
result may also be 0x00?
For example:
#include <stdio.h>
struct bitfield {
int b7:1;
int b6:1;
int b5:1;
int b4:1;
int b3:1;
int b2:1;
int b1:1;
int b0:1;
};
union highlow {
struct bitfield bf;
unsigned char c;
};
int main(void)
{
union highlow h;
h.c = 0;
h.bf.b0 = 1;
if (h.c == 0x80) {
printf("LSB machine\n");
} else {
printf("MSB machine\n");
}
printf("c = %02x\n", h.c);
}