No subject

uunet!homxb.att.com!wilber uunet!homxb.att.com!wilber
Sun Dec 29 12:13:00 PST 1991


Subject: bit fields as long longs

Doug McIlroy writes:
>In recent postings about the width of integers, it seems to have
>been overlooked that C already has syntax for specifying width,
>as exemplified by
>	int a:2;
>No big deal to liberate the syntax from structs or to require
>arithmetic wide enough to accommodate the operands.

I don't think bitfield semantics are what we want.  Consider the following
program:

include <stdio.h>
main()
{
  struct
    {
      unsigned int i:6;
    } foo;
  foo.i = 63;
  foo.i += 5;
  printf("foo.i = %d\n", foo.i);
}
   
When I compile this with gcc and run it the result is:

foo.i = 4

That is, foo.i is *exactly* 6 bits long.  That's what I want in a bit field.
But this implies the use of masking operations when I do arithmetic like:
	foo.i += 5
This is an unacceptable overhead for a primitive integer type.  In other
words, with

	struct { unsigned int i:64 } foo;

foo.i should be exactly 64 bits long, even if this forces the use of masking
operations, but with

	struct { unsigned long long i } foo;

foo.i should be *at least* 64 bits long, but it might be bigger (say, 72 bits
on machines with 36 bit words).

Bob Wilber    wilberahomxb.att.com



More information about the Numeric-interest mailing list