Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!cmcl2!seismo!brl-tgr!tgr!cottrell@nbs-vms.arpa From: cottrell@nbs-vms.arpa (COTTRELL, JAMES) Newsgroups: net.lang.c Subject: Bit Fields in Unions Message-ID: <3352@brl-tgr.ARPA> Date: Mon, 18-Nov-85 15:36:34 EST Article-I.D.: brl-tgr.3352 Posted: Mon Nov 18 15:36:34 1985 Date-Received: Wed, 20-Nov-85 00:58:40 EST Sender: news@brl-tgr.ARPA Lines: 48 /* > 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 */ ------