Fwd: Wrap up question regarding use of bitfields in structures.
Akshay Ranjan
akshay.ranjan@gmail.com
Sat Oct 20 23:12:00 GMT 2007
I have defined a structure like this,
struct CONTROL_MSG_HDR
{
unsigned T :1;
unsigned L :1;
unsigned X1 :2;
unsigned S :1;
unsigned X2:7;
unsigned Ver:4;
unsigned short Length;
unsigned int Id;
unsigned short Ns;
unsigned short Nr;
};
As evident from the definition, fields T, L and S are boolean fields
while field Ver is a 4 bit field which can take maximum value of 0xF.
Now when I assign values other then 0 and 1 to T, L or S field the
compiler throws a warning,
bitfields.c:37: warning: large integer implicitly truncated to
unsigned type
Ditto for Ver field also. But while wrap up of T, L or S field is '0'
when values other than 0 and 1 are assigned, the wrap up value of Ver
is 0x1, if any value greater than 0xF is assigned. See the code
snippet below and the output,
int main(int argc, char *argv[])
{
struct CONTROL_MSG_HDR hdr;
hdr.T = 1;
hdr.L = 2;
hdr.Ver=17;
printf ("sizeof b = %d\n", sizeof(hdr));
printf ("T=%u, L=%u, ver=0x%x\n", hdr.T, hdr.L, hdr.Ver);
return 0;
}
C:\c-prog>gcc -o bitfields.exe bitfields.c
bitfields.c: In function 'main':
bitfields.c:36: warning: large integer implicitly truncated to
unsigned type
bitfields.c:37: warning: large integer implicitly truncated to
unsigned type
C:\c-prog>bitfields.exe
sizeof b = 12
T=1, L=0, ver=0x1
Could anyone pls explain to me this behavior ?
--
Best Regards,
Akshay,
http://akshay.emurse.com/
More information about the Gcc-help
mailing list