sizes, bitfields, etc

Chris Torek chris at mimsy.UUCP
Wed Aug 10 13:12:59 AEST 1988


>In article <1199 at ficc.UUCP> peter at ficc.UUCP (Peter da Silva) writes:
>>Not trying to start a war or anything, but how far do you really have
>>to stretch the language to allow:
>> 	int	a:16;

In article <11794 at steinmetz.ge.com> davidsen at steinmetz.ge.com
(William E. Davidsen Jr) answers:
>I proposed something like this to X3J11 [but size in bytes] ...
>The feeling was that it was (a) not really needed and (b) too much like
>fortran. I like your idea better, but the few cases where you want exact
>size rather than minimum size probably don't justify inclusion.

>It would be nice some times to be able to specify a bit array ....

>I would really like to see a "packed struct," also. This would be a
>struct packed on byte boundaries without fill, no matter *how bad* the
>code was to use them.

I am not sure that ANY of these belong in C (nor that that any or all
do not), but in some language(s) packed structures, bit arrays, and
exact-bit-sized variables would be useful.  If, however, you are going
to provide structure packing, I suggest NOT restricting it to byte
boundaries.  If you need tight packing, the expense for getting at
bit-fields that cross word boundaries is probably worth it.  If you
then can accept looser packing, all you need do is write (e.g.):

	... {
		bits	a:(ROUNDUP(3,BITS_PER_BYTE));
		bits	c:(ROUNDUP(11,BITS_PER_BYTE));
		...

where ROUNDUP is a compile-time expression that rounds its first
argument up to the nearest multiple of its second argument:

	/* nb. beware overflow in (x)+(y)-1 below */
	#define ROUNDUP(x, y)  (floor(((x) + (y) - 1) / (y)) * (y))
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list