Promoting an unsigned bit-field

Walter Murray walter at hpclwjm.HP.COM
Fri Jul 14 07:37:47 AEST 1989


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
-------------



More information about the Comp.std.c mailing list