[net.lang.c] Bit Fields in Unions

sorensen@amdcad.UUCP (Sorensen David) (11/11/85)

	I recently was writing a declaration for a LISP form which looks
something like this:

                    --------------------------------- 
		   |      14-bit integer             |
	---------------------------------------------
       | gc | tag1 |    either ^ or v                |
        ---------------------------------------------
		   | tag2 |   13-bit pointer         |
		    ---------------------------------

	The form is 16 bits wide, with tag1 selecting between the
14-bit integer or 13-bit pointer with another tag. Attempting to
write a C declaration for this:

struct form {
	unsigned int gc : 1;
	unsigned int tag1 : 1;
	union {
		unsigned int number : 14;
		struct {
			unsigned int tag2 : 1;
			unsigned int pointer : 13;
			} pval;
		} val;
	};

	resulted in a "bit-field outside of struct" error, where
number :14 was declared in the union.  I could not find any explicit
mention of bit-fields not being allowed in unions in K & R, but all
C compilers I have tried have not allowed them.  Does anyone know
of a C compiler that allows this?  Or does anyone know why this is not
allowed?

	Tim Olson
	Advanced Micro Devices

jsdy@hadron.UUCP (Joseph S. D. Yao) (11/12/85)

In article <6136@amdcad.UUCP> sorensen@amdcad.UUCP (Sorensen David) writes:
>                    --------------------------------- 
>		   |      14-bit integer             |
>	---------------------------------------------
>       | gc | tag1 |    either ^ or v                |
>        ---------------------------------------------
>		   | tag2 |   13-bit pointer         |
>		    ---------------------------------
>struct form {
>	unsigned int gc : 1;
>	unsigned int tag1 : 1;
>	union {
>		unsigned int number : 14;
>		struct {
>			unsigned int tag2 : 1;
>			unsigned int pointer : 13;
>			} pval;
>		} val;
>	};

(A) Inside a union, you may only use things that can stand
    outside the union.  E.g., int x is OK inside & out; but
    int x:1 is not.  Bit fields must be inside structs.  I'm
    not sure where it says so.
(B) If you are on a "little-endian" machine, like a VAX or
    a PDP-11 (bit 0 is lowest-order), then your order of
    items above is backwards.
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}

cottrell@nbs-vms.arpa (COTTRELL, JAMES) (11/18/85)

/*
> 	I recently was writing a declaration for a LISP form which looks
> something like this:
> 
>                   --------------------------------- 
> 		   |      14-bit integer             |
>         ---------------------------------------------
>        | gc | tag1 |    either ^ or v                |
>         ---------------------------------------------
> 		   | tag2 |   13-bit pointer         |
> 		    ---------------------------------
> 
> 	The form is 16 bits wide, with tag1 selecting between the
> 14-bit integer or 13-bit pointer with another tag. Attempting to
> write a C declaration for this:
> 
> struct form {
> 	unsigned int gc : 1;
> 	unsigned int tag1 : 1;
> 	union {
> 		unsigned int number : 14;
> 		struct {
> 			unsigned int tag2 : 1;
> 			unsigned int pointer : 13;
> 			} pval;
> 		} val;
> 	};
> 
> 	resulted in a "bit-field outside of struct" error, where
> number :14 was declared in the union.  I could not find any explicit
> mention of bit-fields not being allowed in unions in K & R, but all
> C compilers I have tried have not allowed them.  Does anyone know
> of a C compiler that allows this?  Or does anyone know why this is not
> allowed?

The proper declaration is `short thing'. Bit fields and unions are both
stupid. In many years I have always managed to avoid them. Fiddle with
the bits directly as all real programmers do. These should help:

	#define	GC	(1<<15)
	#define TAG1	(1<<14)
	#define TAG2	(1<<13)
	#define MASK13	((1<<13)-1)
	#define MASK14	((1<<14)-1)

	jim		cottrell@nbs
*/
------