H
hmoulding
This may be a FAQ, in which case you all may (probably will) yell at
me.
I haven't coded in plain C for almost 20 years, so I hope the following
code is actually done right.
Suppose the following declaration:
union {
unsigned int uvalue;
unsigned nybbles:4[8];
unsigned bits:1[32];
struct {
unsigned nybble0:4;
unsigned nybble1:4;
unsigned nybble2:4;
unsigned nybble3:4;
unsigned nybble4:4;
unsigned nybble5:4;
unsigned nybble6:4;
unsigned nybble7:4;
} nybblepack;
struct {
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
// ...
} bitpack;
} testorder;
and the following code
testorder.uvalue = 0x12345678;
for ( i=0; i<8 ) printf("nybbles[%i] = %i", i, testorder.nybbles);
for ( i=0; i<32 ) printf("bits[%i] = %i", i, testorder.bits);
printf("nybble0 = %i", testorder.nybblepack.nybble0);
printf("nybble1 = %i", testorder.nybblepack.nybble1);
printf("nybble2 = %i", testorder.nybblepack.nybble2);
printf("nybble3 = %i", testorder.nybblepack.nybble3);
printf("nybble4 = %i", testorder.nybblepack.nybble4);
printf("nybble5 = %i", testorder.nybblepack.nybble5);
printf("nybble6 = %i", testorder.nybblepack.nybble6);
printf("nybble7 = %i", testorder.nybblepack.nybble7);
printf("bit0 = %i", testorder.bitpack.bit0);
printf("bit1 = %i", testorder.bitpack.bit1);
printf("bit2 = %i", testorder.bitpack.bit2);
printf("bit3 = %i", testorder.bitpack.bit3);
printf("bit4 = %i", testorder.bitpack.bit4);
printf("bit5 = %i", testorder.bitpack.bit5);
printf("bit6 = %i", testorder.bitpack.bit6);
printf("bit7 = %i", testorder.bitpack.bit7);
//...
Assuming a standards compliant compiler, would it produce the same
output on all machines? (I suspect not, if for no other reason than
that an int may be larger than what's necessary to store 2^32-1.)
Would it produce the same output on the same machine with different
compliant compilers?
Can anyone refer me to the relevant section in the standard?
me.
I haven't coded in plain C for almost 20 years, so I hope the following
code is actually done right.
Suppose the following declaration:
union {
unsigned int uvalue;
unsigned nybbles:4[8];
unsigned bits:1[32];
struct {
unsigned nybble0:4;
unsigned nybble1:4;
unsigned nybble2:4;
unsigned nybble3:4;
unsigned nybble4:4;
unsigned nybble5:4;
unsigned nybble6:4;
unsigned nybble7:4;
} nybblepack;
struct {
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
// ...
} bitpack;
} testorder;
and the following code
testorder.uvalue = 0x12345678;
for ( i=0; i<8 ) printf("nybbles[%i] = %i", i, testorder.nybbles);
for ( i=0; i<32 ) printf("bits[%i] = %i", i, testorder.bits);
printf("nybble0 = %i", testorder.nybblepack.nybble0);
printf("nybble1 = %i", testorder.nybblepack.nybble1);
printf("nybble2 = %i", testorder.nybblepack.nybble2);
printf("nybble3 = %i", testorder.nybblepack.nybble3);
printf("nybble4 = %i", testorder.nybblepack.nybble4);
printf("nybble5 = %i", testorder.nybblepack.nybble5);
printf("nybble6 = %i", testorder.nybblepack.nybble6);
printf("nybble7 = %i", testorder.nybblepack.nybble7);
printf("bit0 = %i", testorder.bitpack.bit0);
printf("bit1 = %i", testorder.bitpack.bit1);
printf("bit2 = %i", testorder.bitpack.bit2);
printf("bit3 = %i", testorder.bitpack.bit3);
printf("bit4 = %i", testorder.bitpack.bit4);
printf("bit5 = %i", testorder.bitpack.bit5);
printf("bit6 = %i", testorder.bitpack.bit6);
printf("bit7 = %i", testorder.bitpack.bit7);
//...
Assuming a standards compliant compiler, would it produce the same
output on all machines? (I suspect not, if for no other reason than
that an int may be larger than what's necessary to store 2^32-1.)
Would it produce the same output on the same machine with different
compliant compilers?
Can anyone refer me to the relevant section in the standard?