Article View: comp.std.c
Article #328Promoting an unsigned bit-field
From: Walter Murray
Date: Thu, 13 Jul 1989 21:37
Date: Thu, 13 Jul 1989 21:37
37 lines
1769 bytes
1769 bytes
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
-------------
Technical Details
Message-ID:
<12570018@hpclwjm.HP.COM>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!hplabs!hp-ses!hpcuhb!hpcllla!hpclisp!hpclwjm!walterShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!hplabs!hp-ses!hpcuhb!hpcllla!hpclisp!hpclwjm!walter From: walter@hpclwjm.HP.COM (Walter Murray) Newsgroups: comp.std.c Subject: Promoting an unsigned bit-field Message-ID: <12570018@hpclwjm.HP.COM> Date: 13 Jul 89 21:37:47 GMT Organization: Hewlett-Packard Calif. Language Lab Lines: 37