[comp.std.c] Promoting an unsigned bit-field

walter@hpclwjm.HP.COM (Walter Murray) (07/14/89)

It's been a while since we talked about promotion rules, so here's
an easy question for the experts.

What is the result of applying the integral promotion to an unsigned
int bit-field that is narrower than an int?

Specifically, what value is returned by the following function?

   int f (void)
   {
      struct {unsigned int b : 3;} s = {7};
      return s.b/-1;
   }

In other words, does s.b get promoted to int or to unsigned int?

3.2.1.1:  "A char, a short int, or an int bit-field, or their signed
or unsigned varieties, or an object that has enumeration type, may
be used in an expression wherever an int or unsigned int may be used.
If an int can represent all values of the original type, the value is
converted to an int; otherwise it is converted to an unsigned int."

What is the "original type" in this example?

3.5.2.1:  "A bit-field shall have type int, unsigned int, or signed
int. ... A bit-field is interpreted as an integral type consisting
of the specified number of bits."

If the "original type" is 'unsigned int', then the promotion would
be to unsigned int, and the function would return 0.

If the "original type" is 'integral type consisting of 3 bits', then
the original type can only represent values 0 through 7, the promotion
would be to int, and the function would return -7.

Walter Murray
-------------

dfp@cbnewsl.ATT.COM (david.f.prosser) (07/17/89)

In article <12570018@hpclwjm.HP.COM> walter@hpclwjm.HP.COM (Walter Murray) writes:
>It's been a while since we talked about promotion rules, so here's
>an easy question for the experts.
>
>What is the result of applying the integral promotion to an unsigned
>int bit-field that is narrower than an int?
>
>Specifically, what value is returned by the following function?
>
>   int f (void)
>   {
>      struct {unsigned int b : 3;} s = {7};
>      return s.b/-1;
>   }
>
>In other words, does s.b get promoted to int or to unsigned int?

The wording is not extremely clear in the pANS since bit-field is not
effectively a type.  The only evidence on the Committee's intent for
this issue are statements such as following from the Rationale, section
3.2.1.1.

	The unsigned preserving rules greatly increase the number of
	situations where unsigned int confronts signed int to yield a
	questionably signed result, whereas the value preserving rules
	minimize such confrontations.

Given these sorts of statements, the intent is that the size of the bit-
field is taken into account.  In the above example, the promoted type of
s.b would be int, and a signed division should occur.

Dave Prosser	...not an official X3J11 answer...