Bit Fields in Unions
COTTRELL, JAMES
cottrell at nbs-vms.arpa
Tue Nov 19 06:36:34 AEST 1985
/*
> 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 at nbs
*/
------
More information about the Comp.lang.c
mailing list