Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 10 Oct 2004 16:48:17 -0700
From:      "Li, Qing" <qing.li@bluecoat.com>
To:        "Dan Nelson" <dnelson@allantgroup.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   RE: Bit field definition ?
Message-ID:  <00CDF9AA240E204FA6E923BD35BC643606BF695A@bcs-mail.internal.cacheflow.com>

next in thread | raw e-mail | index | archive | help

>=20
> 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.
>=20
>     u_int   th_x2:4,        /* (unused) */
>         th_off:4;       /* data offset */
>=20
> The :4 after each variable means 4 bits long, so both fields=20
> together take up 8 bits =3D 1 byte.  That's the whole purpose=20
> of bitfields :)
>=20
=09
	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,  =20
                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





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?00CDF9AA240E204FA6E923BD35BC643606BF695A>