Bit field definition ?

Li, Qing qing.li at bluecoat.com
Sun Oct 10 16:48:29 PDT 2004


> 
> In the last episode (Oct 08), Li, Qing said:
> > 	The bit fields "th_x2" and "th_off" in "struct tcphdr",
> > 	even though defined as "u_int", actually occupies 1 byte.
> 
>     u_int   th_x2:4,        /* (unused) */
>         th_off:4;       /* data offset */
> 
> The :4 after each variable means 4 bits long, so both fields 
> together take up 8 bits = 1 byte.  That's the whole purpose 
> of bitfields :)
> 
	
	D'oh

	I didn't ask the right question.

	It seems u_int specifies the packing and alignment size
	for the bit fields, is that correct ?

	struct {
          u_int a:4,   
                b:4;
	};               is 4 bytes in size.

	struct {
         u_int a:4,
               b:4;
         short c;
      };               is 4 bytes in size.

      struct {
         u_int a:4,
               b:4;
         short c;
         u_char d;
      };               is 8 bytes in size;

      But

      struct {
         u_int a:4,
               b:4;
         u_char d;
         short c;
      };               is 4 bytes in size;


	-- Qing





More information about the freebsd-hackers mailing list