Bitfield syntax for widths of integral types

uunet!research.att.com!doug uunet!research.att.com!doug
Mon Dec 30 10:17:38 PST 1991


1.
> Do you propose that we could make a type of that form, e.g.
>
>	typedef long sixtyfour:64;

Absolutely.  This is just a form of declarator.

2.
> I don't think bitfield semantics are what we want.

Agreed.  Usually we don't.  This is the kind of concern I had
in mind in speaking of "liberating" the syntax from structs.
A simple way to cope would be a "storage round" operator that
gives smallest embracing natural size:

	typedef int int6:sround(6);	/* int6 == char */

Storage rounding would apply implicitly to parameters
of integral type.

3.
> How can we use it simultaneously on a bunch of variables:
>
>	short a, b, c;
>	int a:64, b, c;		doesn't work.

True.  But typedef does.  The same situation occurs in specifying a
bunch of arrays of equal size.

4.
Bitfield and array notation should be combinable:

	unsigned a[16]:1;	/* packed, 1-bit quantities */
	unsigned b[16]:sround(1);	/* aligned, 8-bit */

Note that array a could not be passed as an argument except within
a structure.  Array b, which is just an array of unsigned
chars, could be passed.

5.
One would also like a bit-size operator:

	bsizeof(x)	/* the number of bits allotted to x */
	bsizeof(6)	/* the number of bits necessary to hold
			   the (signed) constant 6 */
	bsizeof(char)	/* the number of bits in a char */

in terms of which sizeof may almost be defined.  The following
identity holds unless x is an integral constant:
	
	sizeof(x) == sround(bsizeof(x))/bsizeof(char)

6.
Nasties.  ANSI allows int bitfields, like chars, to be signed or
unsigned by default, while simple ints are signed by default.
Clearly generalized bitfields should be more like ints than
like today's specialized bitfields.

ANSI also specifies that bitfields in structs are packed.
This could make the two structs below different when
chars are 8 bits and shorts are 16.  A possible compromise:
pack natural-size bitfields whose sizes are given explicitly in
a struct declaration; otherwise align them.
	
	typedef int int16:16;
	typedef int int8:8;
	struct { int8 a; int16 b; int8 c; };
	struct { char a; short b; char c; };



More information about the Numeric-interest mailing list